非类型模板参数

         非类型模板参数大体来说可以是整数,枚举,具有外部链接的对象或函数地址,不可以是浮点数,对象或者内部链接变量。例子如下:

//整数
template<class S, int T>
class Test1
{
	S mMem;
};
Test1<int, 3> test1;

//字符串
char pc[]="hello world";
//const char pc[]="hello world";//error 默认static内部变量
//extern const char pc[]="hello world";//right extern转为外部变量
template<class S, const char* T>
class Test2
{
	S mMem;
};
Test2<int, pc> test2;

//枚举
enum EM{em1,em2,em3};
template<class S, EM T>
class Test3
{
	S mMem;
};
Test3<int, em1> test3;

//类成员函数指针
class A
{
public:
	virtual void fun(){}
};
typedef void (A::*AF)();
template<class S, AF T>
class Test4
{
	S mMem;
};
Test4<int, &A::fun> test4;

//具有外部链接属性的函数的地址
void fun(){}
typedef void (*AFF)();
template<class S, AFF T>
class Test5
{
	S mMem;
};
Test5<int, &fun> test5;

//具有外部链接属性的对象的地址
A a;
template<class S, A* T>
class Test6
{
	S mMem;
};
Test6<int, &a> test6;

//double指针
template<typename T, double *x, double &y> 
T Test7( T a )
{
	return a;
}
double x;
int b = Test7<int, &x, x>(1);

        听说c++11把标准放宽了,可以是一些内部链接变量,暂未研究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值