fork(1) download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. struct has_typedef_foobar {
  5. // Types "yes" and "no" are guaranteed to have different sizes,
  6. // specifically sizeof(yes) == 1 and sizeof(no) == 2.
  7. typedef char yes[1];
  8. typedef char no[2];
  9.  
  10. template <typename C>
  11. static yes& test(typename C::foobar*);
  12.  
  13. template <typename>
  14. static no& test(...);
  15.  
  16. // If the "sizeof" of the result of calling test<T>(nullptr) is equal to sizeof(yes),
  17. // the first overload worked and T has a nested type named foobar.
  18. static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
  19. };
  20.  
  21. struct foo {
  22. typedef float foobar;
  23. };
  24.  
  25. int main() {
  26. std::cout << std::boolalpha;
  27. std::cout << has_typedef_foobar<int>::value << std::endl; // Prints false
  28. std::cout << has_typedef_foobar<foo>::value << std::endl; // Prints true
  29. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
false
true