fork download
  1. #include <iostream>
  2.  
  3. template <typename Type>
  4. class has_member
  5. {
  6. class yes { char m;};
  7. class no { yes m[2];};
  8. struct BaseMixin
  9. {
  10. void operator()(){}
  11. };
  12. struct Base : public Type, public BaseMixin {};
  13. template <typename T, T t> class Helper{};
  14. template <typename U>
  15. static no deduce(U*, Helper<void (BaseMixin::*)(), &U::operator()>* = 0);
  16. static yes deduce(...);
  17. public:
  18. static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0)));
  19. };
  20.  
  21. struct A{};
  22. struct B{ void operator()(){} };
  23. struct C{ void operator()(int,int){} };
  24.  
  25. int main()
  26. {
  27.  
  28. std::cout << std::boolalpha; //so that true or false will be printed instead of 1 or 0
  29.  
  30. std::cout << has_member<A>::result << std::endl;
  31. std::cout << has_member<B>::result << std::endl;
  32. std::cout << has_member<C>::result << std::endl;
  33. }
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
false
true
true