fork(9) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename T, typename = int>
  5. struct HasHelloFunction : std::false_type {};
  6.  
  7. template<typename T>
  8. struct HasHelloFunction<T, decltype((void)T::hello(), 0)> : std::true_type {};
  9.  
  10. struct Works
  11. {
  12. static int hello() {}
  13. };
  14.  
  15. struct Works2
  16. {
  17. static void hello() {}
  18. };
  19.  
  20. struct DoesntWork
  21. {
  22. static int hello; // close but not callable
  23. };
  24.  
  25. int main() {
  26.  
  27. cout << "char: " << HasHelloFunction<char>::value << endl;
  28. cout << "Works: " << HasHelloFunction<Works>::value << endl;
  29. cout << "Works2: " << HasHelloFunction<Works2>::value << endl;
  30. cout << "DoesntWork: " << HasHelloFunction<DoesntWork>::value << endl;
  31. cout << "int: " << HasHelloFunction<int>::value << endl;
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
char: 0
Works: 1
Works2: 1
DoesntWork: 0
int: 0