type_traits

#include 

namespace namespace269 {
    
    /** 2两个空白类,没有任何成员,不会带来额外负担,却能通过类型代表真假,可以用于函数特化参数 */
    struct __true_type { };
    struct __false_type { };
    
    template 
    struct __type_traits {
        typedef __true_type this_dummy_member_must_be_first;
      

        typedef __false_type has_trivial_default_constructor;
        typedef __false_type has_trivial_copy_constructor;
        typedef __false_type has_trivial_assignment_operator;
        typedef __false_type has_trivial_destructor;
        typedef __false_type is_POD_type;
    };
}

template 
void test_tpye_traits(T& t, namespace269::__true_type) {
    std::cout << "true" << std::endl;
}

template 
void test_tpye_traits(T& t, namespace269::__false_type) {
    std::cout << "false" << std::endl;
}

template 
void test_tpye_traits(T& t) {
    typedef typename namespace269::__type_traits::is_POD_type is_POD_type;    
    test_tpye_traits(t, typename namespace269::__type_traits::is_POD_type());
}


class A {
public:
};

class B {
};


namespace namespace269 {
    template <>
    struct __type_traits {
        typedef __true_type is_POD_type;
    };

    template <>
    struct __type_traits {
        typedef __false_type is_POD_type;
    };
}

int main(void) {
    A a;
    B b;
    test_tpye_traits(a);   // true
    test_tpye_traits(b);   // false

    return 0;
}

你可能感兴趣的:(C/C++)