模板

//判断同一类型
template<typename T,typename U>
struct same_type
{
static const bool value = false;
};
template<typename T>
struct same_type<T,T>
{
static const bool value = true;
};
char test_func()
{
   return ''; //error, return empty char constant
   return ' '; //ok

}


/ *特化*/

    // dummy type, don't define
    struct smart_any_cannot_dereference;

    // For use in implementing unary operator*
    template<typename T>
    struct deref
    {
        typedef smart_any_cannot_dereference type; // will cause a compile error by default
    };

在特化前必须有模板的完全定义
template<typename T>
struct deref<T*>
{
typedef T& type; // The result of dereferencing a T*
const static int value = 123; 
};


/一个特殊的类型/

  template<typename T>
    struct has_unused
    {
    private:

        template<class U,int U::*> struct wrap_t;
        template<typename U> static yes_t check( wrap_t<U,&U::unused>* );

        template<typename U> static no_t  __cdecl check( ... );
    public:
        static bool const value = ( sizeof(check<T>(0)) == sizeof(yes_t) );
    };

        //要使value返回true,U必须有一个成员变量unused

struct HANDLE_

{

     int unused;

};

同时,sizeof(check<T>(0))虽会返回对应的check函数return type,但函数不会被调用,我查看过汇编代码,直接显示的返回类型的大小,没有函数调用


/类型安全/

template<bool>
struct static_assert;

template<>
struct static_assert<true>
{
static_assert() {}
};

//使用方法如下:

static_assert<0 != sizeof(T)> no_complete_type

由于没有定义struct static_assert<false>{};类型,编译会出错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值