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