stl源码——萃取器

萃取机制

namespace
{
    template<bool, class Ta, class Tb>
    class IfThenElse; // 声明

    // 偏特化,true为默认
    template<class Ta, class Tb>
    struct IfThenElse<true, Ta, Tb>
    {
        using result = Ta; // res 表示Ta类型,结果类型由泛型传入
    };
} // namespace 

class _true_type {};
class _false_type {};

基本模板

// 默认自定义类型为non_pod类型 ====~类类型
// 非 plain old data 
template <class T>
struct _type_traits
{
    // trival  需要重写,不提供
    using has_trival_default_constructor = _false_type;
    using has_trival_copy_constructor = _false_type;
    using has_trival_assignment_operator = _false_type;
    using has_trival_destructor = _false_type;
    using is_POD_type = _false_type;  // 不是pod  而是类
};

// 对基本类型进行偏特化
// 基本类型都有足够的ctor dtor = 不需要重写
template <>
struct _type_traits<bool>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 

特化、偏特化

template <>
struct _type_traits<unsigned char>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<signed char>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<wchar_t>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<short>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<unsigned short>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<long>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<long long>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<unsigned long>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<unsigned long long>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<float>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<double>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<long double>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 

// 指针偏特化
template <class T>
struct _type_traits<T*>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <class T>
struct _type_traits<const T*>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 

// 特化
template <>
struct _type_traits<char*>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
}; 
template <>
struct _type_traits<const char*>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
};
template <>
struct _type_traits<unsigned char*>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
};
template <>
struct _type_traits<signed char*>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
};
template <>
struct _type_traits<const unsigned char*>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
};
template <>
struct _type_traits<const signed char*>
{
    using has_trival_default_constructor = _true_type;
    using has_trival_copy_constructor = _true_type;
    using hash_trival_assignment_operator = _true_type;
    using has_trivial_destructor = _true_type;
    using is_POD_type = _true_type;
};
    template <class T>
    struct _is_integer
    {
        // 默认自定义类型是false----需要重写ctor dtor
        using _integral = _false_type;
    };
    template <class T>
    using _is_integer_t = typename _is_integer<T>::_integral;

    template<>
    struct _is_integer<bool>
    {
        using _integral = _true_type;
    };

    template<>
    struct _is_integer<char>
    {
        using _integral = _true_type;
    };

    template<>
    struct _is_integer<signed char>
    {
        using _integral = _true_type;
    };

    template<>
    struct _is_integer<unsigned char>
    {
        using _integral = _true_type;
    };
    template<>
    struct _is_integer<wchar_t>
    {
        using _integral = _true_type;
    };

    template<>
    struct _is_integer<short>
    {
        using _integral = _true_type;
    };
    template<>
    struct _is_integer<unsigned short>
    {
        using _integral = _true_type;
    };

    template<>
    struct _is_integer<int>
    {
        using _integral = _true_type;
    };
    template<>
    struct _is_integer<unsigned int>
    {
        using _integral = _true_type;
    };

    template<>
    struct _is_integer<long>
    {
        using _integral = _true_type;
    };
    template<>
    struct _is_integer<unsigned long>
    {
        using _integral = _true_type;
    };

    template<>
    struct _is_integer<long long>
    {
        using _integral = _true_type;
    };
    template<>
    struct _is_integer<unsigned long long>
    {
        using _integral = _true_type;
    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值