C++ Template之非类型模板参数

非类型模板参数是通过基本变量类型引入,例如int,在使用时必须显式自定值,不能通过推断。

非类型模板参数的限制:不能是浮点数(在vc6.0上测试可以为浮点型),对象以及指向内部链接对象的指针。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

enum COLOR{WHITE,BLACK};
template<COLOR name>//OK
int process (double v)
{
	return v*name;
}
template <const char* s>
class Myclass
{

};
const char s1[] = "hello";
extern const char s2[] = "hello";
int main()
{

	//Myclass<s1> m1;//ERROR 指向内部链接对象的指针
	//Myclass<"hello"> m2;
	Myclass<s2> m3;//OK指向外部链接对象的指针
	return 0;
}

 

 

posted @ 2013-07-24 23:25  l851654152  阅读(3799)  评论(0编辑  收藏  举报