With reference to
the question relates to the following code segment:
template<typename T>
class is_class {
typedef char yes[1];
typedef char no [2];
template<typename C> static yes& test(int C::*); // selected if C is a class type
template<typename C> static no& test(...); // selected otherwise
public:
static bool const value = sizeof(test<T>(0)) == sizeof(yes);
};
In the above class template definition, the public
definition for is_class<T>::value
instantiates the nested static template function test
using the parameter T
used in the instantiation of the class template is_class
.
If the above interpretation is correct, I have the following questions:
- How does one interpret the overloaded versions of the the nested static template function
test
?
How does SFINAE work using the above overloads to populate the public boolean variable value
of template class is_class<T>
for a given T
, with true or false as applicable, based on sizeof(test<T>(0)) == sizeof(yes)
? I am not sure I understand how passing what appears to be a random integer value 0
in the instantiation of test<T>
should either choose or eliminate either overload? Can someone provide an explanation with an example?
- The logic of using two nested types of
yes[1]
andno [2]
, both of which have been typedef'd tochar
is also not clear to me. Assuming thatyes[1]
is a type alias for a char array with size 1, andno[2]
a type alias for a char array with size 2, how does this logic integrate into the overload selection/elimination using SFINAE?
Appreciate your thoughts.