fork(7) download
  1. #include <type_traits>
  2. #include <iostream>
  3.  
  4.  
  5. // Our test candidates:
  6. struct i_have_foo { void foo() const {} };
  7. struct i_dont { void bazinga() const {} };
  8. int bar(std::ostream) {}
  9.  
  10.  
  11. // First checker:
  12. struct _test_has_foo {
  13. template<class T>
  14. static auto test(T* p) -> decltype(p->foo(), std::true_type());
  15.  
  16. template<class>
  17. static auto test(...) -> std::false_type;
  18. };
  19. template<class T>
  20. struct has_foo : decltype(_test_has_foo::test<T>(0))
  21. {};
  22.  
  23.  
  24. // Second checker:
  25. struct _test_is_supported_by_bar {
  26. template<class T>
  27. static auto test(T* p) -> decltype(bar(*p), std::true_type());
  28.  
  29. template<class>
  30. static auto test(...) -> std::false_type;
  31. };
  32. template<class T>
  33. struct is_supported_by_bar : decltype(_test_is_supported_by_bar::test<T>(0))
  34. {};
  35.  
  36.  
  37. // Test:
  38. int main()
  39. {
  40. std::cout << has_foo<i_have_foo>::value << std::endl;
  41. std::cout << has_foo<i_dont>::value << std::endl;
  42.  
  43. std::cout << std::endl;
  44.  
  45. std::cout << is_supported_by_bar<std::ostream>::value << std::endl;
  46. std::cout << is_supported_by_bar<std::istream>::value << std::endl;
  47. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
1
0

0
0