對以判斷是不是某個基本類型(整數, 浮點, 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