fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. using namespace std;
  5.  
  6. struct FooNo {};
  7. struct FooYes { int x; };
  8.  
  9. struct FooDerivedYes : public FooYes {};
  10. struct FooDerivedNo : public FooNo {};
  11. struct FooDerivedTwiceYes final : public FooYes { int x; };
  12.  
  13. template <typename T, typename = void>
  14. struct HasX : false_type { };
  15.  
  16. template <typename T>
  17. struct HasX <T, decltype((void) T::x)> : true_type { };
  18.  
  19. int main() {
  20. cout << HasX<FooYes>::value << endl;
  21. cout << HasX<FooNo>::value << endl;
  22. cout << HasX<FooDerivedYes>::value << endl;
  23. cout << HasX<FooDerivedNo>::value << endl;
  24. cout << HasX<FooDerivedTwiceYes>::value << endl;
  25. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1
0
1
0
1