fork download
  1. #include <cstdint>
  2. #include <iostream>
  3.  
  4. #define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \
  5.   template <typename U> \
  6.   class traitsName \
  7.   { \
  8.   private: \
  9.   template<typename T, T> struct helper; \
  10.   template<typename T> \
  11.   static std::uint8_t check(helper<signature, &funcName>*); \
  12.   template<typename T> static std::uint16_t check(...); \
  13.   public: \
  14.   static \
  15.   constexpr bool value = sizeof(check<U>(0)) == sizeof(std::uint8_t); \
  16.   }
  17.  
  18. DEFINE_HAS_SIGNATURE(has_ham_const, T::ham, void (T::*)() const);
  19.  
  20.  
  21. struct foo {
  22. void ham() {}
  23. void ham() const {}
  24. };
  25.  
  26. struct bar {
  27. void ham() {}
  28. };
  29.  
  30. static_assert(has_ham_const<foo>::value, "unexpected");
  31. static_assert(!has_ham_const<bar>::value, "unexpected");
  32.  
  33. int main()
  34. {
  35. std::cout << has_ham_const<foo>::value << std::endl; // 1
  36. std::cout << has_ham_const<bar>::value << std::endl; // 0
  37. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
1
0