非类型模板参数

来源:互联网 发布:中国取黑人媳妇知乎 编辑:程序博客网 时间:2024/05/22 08:26

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

//整数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把标准放宽了,可以是一些内部链接变量,暂未研究。

0 0
原创粉丝点击