fork download
  1. struct A {
  2. bool toBool() const { return true; }
  3. template<typename T> T to() const { return T();}
  4. };
  5.  
  6. template<typename From, typename To>
  7. class has_to_func
  8. {
  9. typedef char (&Two)[2];
  10. template<typename F, To (F::*)() const> struct helper {};
  11. template<typename F> static char test(helper<F, &F::template to<To> >*);
  12. template<typename F> static Two test(...);
  13. public:
  14. static const bool value = (sizeof(test<From>(0)) == sizeof(char));
  15. };
  16.  
  17. #include <iostream>
  18.  
  19. int main()
  20. {
  21. ::std::cout << "int: " << has_to_func<int, bool>::value
  22. << ", A: " << has_to_func<A, bool>::value << ::std::endl;
  23. }
  24.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
int: 0, A: 1