c++ - Why SFINAE trick doesn't work for non-class type when tried for class member pointer? -
with curiosity, trying alternate implementation of is_class construct using sizeof() trick. following code:
template<typename t> struct is_class { typedef char (&yes)[7]; typedef char (&no)[3]; static yes check (int t::*); static no check (...); enum { value = (sizeof(check(0)) == sizeof(yes)) }; }; problem when instantiate is_class<int>, gives compile error:
error: creating pointer member of non-class type ‘int’ now, question is, if int t::* not applicable int (or void* etc.) why doesn't substitution fail yes check. shouldn't compiler select no check ?
yes , no not templates, sfinae cannot possibly apply them. need this:
template<typename t> struct is_class { typedef char (&yes)[7]; typedef char (&no)[3]; template <typename u> static yes check (int u::*); template <typename> static no check (...); enum { value = (sizeof(check<t>(0)) == sizeof(yes)) }; }; now sfinae can kick in.
Comments
Post a Comment