fork download
  1. #include <iostream>
  2.  
  3. struct true_type
  4. {
  5. enum {value = 1};
  6. };
  7.  
  8. struct false_type
  9. {
  10. enum {value = 0};
  11. };
  12.  
  13. template<typename T>
  14. struct is_integer_impl : false_type {};
  15.  
  16. #define DECLARE_INTEGER(T) \
  17. template<> \
  18. struct is_integer_impl<T> : true_type {}
  19.  
  20. DECLARE_INTEGER(char);
  21. DECLARE_INTEGER(unsigned char);
  22. DECLARE_INTEGER(signed char);
  23. DECLARE_INTEGER(unsigned short);
  24. DECLARE_INTEGER(signed short);
  25. DECLARE_INTEGER(unsigned int);
  26. DECLARE_INTEGER(signed int);
  27. DECLARE_INTEGER(unsigned long);
  28. DECLARE_INTEGER(signed long);
  29.  
  30. template<typename T>
  31. struct is_integer : is_integer_impl<T> {};
  32.  
  33. template<typename T>
  34. struct is_class
  35. {
  36. typedef char yes;
  37. typedef int no;
  38.  
  39. template<class U>
  40. static yes doCheck(void (U::*)());
  41. template<class U>
  42. static no doCheck(...);
  43.  
  44. enum {value = (sizeof(doCheck<T>(0)) == sizeof(yes))};
  45. };
  46.  
  47. template<typename T>
  48. struct is_enum
  49. {
  50. enum {value = !is_integer<T>::value && !is_class<T>::value};
  51. };
  52.  
  53. enum TestEnum
  54. {
  55. Item1,
  56. Item2
  57. };
  58.  
  59. struct TestStruct
  60. {
  61.  
  62. };
  63.  
  64. union TestUnion
  65. {
  66. };
  67.  
  68. int main()
  69. {
  70. std::cout << is_integer<int>::value << std::endl;
  71. std::cout << is_enum<int>::value << std::endl;
  72. std::cout << is_class<int>::value << std::endl;
  73. std::cout << std::endl;
  74. std::cout << is_integer<TestStruct>::value << std::endl;
  75. std::cout << is_enum<TestStruct>::value << std::endl;
  76. std::cout << is_class<TestStruct>::value << std::endl;
  77. std::cout << std::endl;
  78. std::cout << is_integer<TestEnum>::value << std::endl;
  79. std::cout << is_enum<TestEnum>::value << std::endl;
  80. std::cout << is_class<TestEnum>::value << std::endl;
  81. std::cout << std::endl;
  82. std::cout << is_integer<TestUnion>::value << std::endl;
  83. std::cout << is_enum<TestUnion>::value << std::endl;
  84. std::cout << is_class<TestUnion>::value << std::endl;
  85.  
  86. return 0;
  87. }
Success #stdin #stdout 0s 4448KB
stdin
Standard input is empty
stdout
1
0
0

0
0
1

0
1
0

0
0
1