I'm trying to wrap my head around SFINAE. We're using it to check whether a class has a method called "Passengers".
With some online examples, we constructed the following template classes.
#ifndef TYPECHECK
#define TYPECHECK
#include "../Engine/carriage.h"
namespace TSS{
template<typename T>
class has_passengers{
private:
typedef char one;
typedef struct{char a[2];} two;
template<typename C> static one test( decltype(&C::Passengers) );
template<typename C> static two test(...);
public:
static bool const value = sizeof(test<T>(0)) == sizeof(one);
};
template<typename T>
struct CarriageTypeCheck{
static_assert(has_passengers<T>::value, "Train initialized with illegal carriage");
};
}
#endif // TYPECHECK
I get the part how either of the two test-methods is chosen, but what I don't understand is why test<T>
is initialized with 0 in the following line:
static bool const value = sizeof(test<T>(0)) == sizeof(one);
I can not see how the 0 is important for the check to work. Another thing - why is decltype used?