c++ - How to determine if a class contains a subclass / type? -
can have sfinae trick know, if class has subclass/type. like,
template<typename type> // searches "my_type" struct has_inner_type { enum { value = <???> }; };
following examples:
struct { class my_type {}; // has_inner_type::value = true }; struct b { }; // has_inner_type::value = false struct c { typedef int my_type; }; // has_inner_type::value = true
i tried few tricks, falling short expected compiler errors. usage:
bool b = has_inner_type<a>::value; // respect "my_type"
edit: have re-edited question, seems it's impossible pass my_type
second parameter has_inner_type
. so, of question find specific type my_type
. have tried code, doesn't work.
following answer present in wikipedia link posted in question !! (thanks @n.m.)
template <typename t> struct has_inner_type { typedef char yes[1]; typedef char no[2]; template <typename c> static yes& test(typename c::my_type*); template <typename> static no& test(...); static const bool value = sizeof(test<t>(0)) == sizeof(yes); };
here the demo.
Comments
Post a Comment