traits

traits是一种“可于编译期根据类型作判断”的范型技术。 

它可以进行指针、引用和成员变量指针识别,我们如此使用指针:

int*    pa;
int&    rb;
int*    cls::pmc;    //注意这里定义类成员指针的方法,你在卸去它的时候会好理解很多。

其实识别你可以认为是一种卸载:

template    <class U>    struct    PointerTraits<U*>
{
    
enum    {result    = true};
    typedef    U    PointerType;
}
;

template    
<class U>    struct    ReferenceTraits<U&>
{
    
enum    {result    = true};
    typedef    U    ReferenceType;
}
;

template    
<class U,class V>    struct    MPointerTraints<U V::*>
{
    
enum    {result = true};
    typedef    U    MPointerType;
}

2.依此和typelist你可以做出基本类型识别

3.依卸载的思想,卸去const装饰词也不是什么问题

template    <typename    T>
class    TypeTaits
{
private:
    template    
<class U>    struct    UnConst
    
{
        typedef    U    Result;
    }
;
    template    
<class U>    struct    UnConst<const U>
    
{
        typedef    U    Result;
    }
;
public:
    typedef    UnConst
<T>::Result    NonConstType;
}
;

ps我发现我越来越喜欢代码说明问题了。我所想只想用代码来表示,我觉得那是我思维的生化,但是我自己都是看代码不认真的人,所以我知道这样一样让人难以理解。但是我尽量安装我的思维顺序组织代码,以必要的注释表明我说想。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值