天下

记录修行的印记

c++ traits(1)

#pragma once
template 
<typename T>  
struct TraitsHelper;

template 
<typename T>  
struct TraitsHelper {  
    
static const bool isPointer = false;  
    
enum {IsPointer = false};
    
bool GetIsPointer()
    {
        
return IsPointer;
    }
};  
template 
<typename T>  
struct TraitsHelper<T*> {  
    
static const bool isPointer = true;  
    
enum {IsPointer = true};
    
bool GetIsPointer()
    {
        
return IsPointer;
    }
};  

//call
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
    
int val = 0;
    
int* pVal = &val;
    TraitsHelper
<int> a;
    TraitsHelper
<int*> b;

    
bool b1 = a.IsPointer;
    
bool b2 = b.IsPointer;

    
bool b3 = a.GetIsPointer();

    
bool b4 = TraitsHelper<int>::IsPointer;
    
bool b5 = TraitsHelper<int*>::IsPointer;


    
return 0;
}

posted on 2016-10-14 14:18 天下 阅读(435) 评论(0)  编辑 收藏 引用 所属分类: C/C++C++模板


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2016年10月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论