fork download
  1. #include <cstdint>
  2. #include <iostream>
  3.  
  4. template <typename U>
  5. class has_special
  6. {
  7. private:
  8. template<typename T, typename = typename T::special>
  9. static std::uint8_t check(int);
  10. template<typename T> static std::uint16_t check(...);
  11. public:
  12. static
  13. constexpr bool value = sizeof(check<U>(0)) == sizeof(std::uint8_t);
  14. };
  15.  
  16. struct NormalType { };
  17. struct SpecialType { typedef int special; };
  18.  
  19. static_assert(has_special<SpecialType>::value, "");
  20. static_assert(!has_special<NormalType>::value, "");
  21.  
  22. template <typename T, bool = has_special<T>::value>
  23. struct DetectSpecial {
  24. void detected() { std::cout << "Not special...\n"; }
  25. };
  26.  
  27. template <typename T>
  28. struct DetectSpecial<T, true> {
  29. void detected() { std::cout << "Special!\n"; }
  30. };
  31.  
  32. int main()
  33. {
  34. DetectSpecial<NormalType>().detected();
  35. DetectSpecial<SpecialType>().detected();
  36.  
  37. return 0;
  38. }
  39.  
  40.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Not special...
Special!