fork(19) download
  1. #include <cstddef>
  2. #include <string>
  3. #include <type_traits>
  4.  
  5. template <class C>
  6. class HasGreetMethod
  7. {
  8. template <class T>
  9. static std::true_type testSignature(void (T::*)(const char*) const);
  10.  
  11. template <class T>
  12. static decltype(testSignature(&T::greet)) test(std::nullptr_t);
  13.  
  14. template <class T>
  15. static std::false_type test(...);
  16.  
  17. public:
  18. using type = decltype(test<C>(nullptr));
  19. static const bool value = type::value;
  20. };
  21.  
  22. struct A
  23. {
  24. void greet(const char* name) const;
  25. };
  26.  
  27. struct Derived : A { };
  28.  
  29. struct B
  30. {
  31. void greet(std::string name) const;
  32. };
  33.  
  34. static_assert(HasGreetMethod<A>::value, "");
  35. static_assert(HasGreetMethod<Derived>::value, "");
  36. static_assert(!HasGreetMethod<B>::value, "");
  37.  
  38. int main()
  39. {
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 3092KB
stdin
Standard input is empty
stdout
Standard output is empty