Nontype template parameters的局限
- 通常来说它们只能是常数整数(const integral values),包括enum,或是【指向外部连接(external linkage)之物件】的指标
- 浮点数或者class-type object 作为nontype template parameters是不可以的
template<deouble VAT> /*error, double is not a valid type for template parameter*/
double process(double v)
{
return v * VAT;
}
template <const char* name>
class MyClass
{...}
const char* s = "hello";
MyClass<"hello"> x; /*error, cant't use string constant*/
MyClass<s> x; /*error, s is point to internal linkage*/
extern const char s1[] = "hello";
MyClass<s1> x; //OK
- Templates parameters 不限只能是型别,也可以是数值
- 你不能把浮点数、class-type物件、内部连接物件(internal linkage)当作nontype template parameters的引数
- 什么是外部连接物件,什么是内部连接物件[http://blog.csdn.net/xiexievv/article/details/8487373]
本文探讨了C++中非类型模板参数的使用限制,如必须为常数整数或指向外部连接物件的指标,不能使用浮点数、class-type物件等,并通过实例说明如何正确指定这些参数。

227

被折叠的 条评论
为什么被折叠?



