type_traits

#include <iostream>

namespace namespace269 {
    
    /** 2两个空白类,没有任何成员,不会带来额外负担,却能通过类型代表真假,可以用于函数特化参数 */
    struct __true_type { };
    struct __false_type { };
    
    template <class _Tp>
    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 <class T>
void test_tpye_traits(T& t, namespace269::__true_type) {
    std::cout << "true" << std::endl;
}

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

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


class A {
public:
};

class B {
};


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

    template <>
    struct __type_traits<B> {
        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;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值