fork(1) download
  1. #include <type_traits>
  2.  
  3. template <typename T>
  4. std::true_type call_foo (int (T::*)(float));
  5.  
  6. template <typename C>
  7. decltype(call_foo(&C::foo)) has_foo (int);
  8.  
  9. template <typename C>
  10. std::false_type has_foo (...);
  11.  
  12.  
  13. template<typename T>
  14. using HasFoo = decltype(has_foo<T>(0));
  15.  
  16. struct A {
  17. int foo(float);
  18. };
  19.  
  20. struct B : public A {
  21. };
  22.  
  23. struct C {
  24. unsigned int foo(double);
  25. };
  26.  
  27. struct D {
  28. static int foo(float);
  29. };
  30.  
  31. static_assert(HasFoo<A>::value, "A should have foo.");
  32. static_assert(HasFoo<B>::value, "B should inherit foo from A.");
  33.  
  34. static_assert(!HasFoo<C>::value, "C should not have foo.");
  35. static_assert(!HasFoo<D>::value, "Ds static foo should be false.");
  36.  
  37. struct E {};
  38. static_assert(!HasFoo<E>::value, "E does not have foo.");
  39.  
  40. int main () {
  41.  
  42. }
  43.  
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
Standard output is empty