对以判断是不是某个基本类型(整数, 浮点, bool)可以参考下面代码:
3. is_float
- template<typename T>
- struct is_float : bool_type<false>{};
- #define IS_FLOAT(T) template<> struct is_float<float> : bool_type<true>{};/
- template<> struct is_float<const float> : bool_type<true>{};/
- template<> struct is_float<volatile float> : bool_type<true>{};/
- template<> struct is_float<const volatile float> : bool_type<true>{}
- IS_FLOAT(float);
- IS_FLOAT(double);
- IS_FLOAT(long double);
4. is_calss
判断一个类型T是不是类可以判断是不是存在 void(T::*)(void)类型的成员函数, 当然这里并不需要真存在。
由于对struct, class这些都是满足的, 所以并不能吧struct和class中区分出来。
- typedef char yes_type;
- typedef int no_type;
- template<typename T>
- struct is_class_imp{
- template <typename U> static yes_type is_calss_tester(void(U::*)(void));
- template <typename U> static no_type is_calss_tester(...);
- static const bool value = (sizeof(is_calss_tester<T>(0)) == sizeof(yes_type));
- };
- template<typename T>
- struct is_class : is_class_imp<T>{};
- struct A{};
- class B{};
- enum C{};
- int main()
- {
- bool is_1 = is_class<int>::value; //false
- bool is_2 = is_class<A>::value;
- bool is_3 = is_class<B>::value;
- bool is_4 = is_class<C>::value; //false
- bool is_6 = is_class<const A>::value;
- }
后面回接着讲述
is_function
is_member_function_pointer
is_member_object_pointer
is_member_pointer