If you can use C ++ 11, this is completely trivial:
template<class T> struct has_nested_option{ typedef char yes; typedef yes (&no)[2]; template<class U> static yes test(decltype(U::option)*); template<class U> static no test(...); static bool const value = sizeof(test<T>(0)) == sizeof(yes); };
The C ++ 03 version (surprisingly) is similar:
template<class T> struct has_nested_option{ typedef char yes; typedef yes (&no)[2]; template<int> struct test2; template<class U> static yes test(test2<U::option>*); template<class U> static no test(...); static bool const value = sizeof(test<T>(0)) == sizeof(yes); };
Using:
struct foo{ enum { option = 1 }; }; struct bar{}; #include <type_traits> template<class T> typename std::enable_if< has_nested_option<T>::value >::type Do(){ } int main(){ Do<foo>(); Do<bar>(); // error here, since you provided no other viable overload }
source share