非類型模板參數是通過基本變量類型引入,例如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;
}
