【发布时间】:2013-01-10 01:28:51
【问题描述】:
虽然 C++ 标准不允许使用字符串文字作为模板参数,但允许使用以下内容:
ISO/IEC 14882:2011
14.3.2 模板非类型参数 [temp.arg.nontype]
2 [注意:字符串文字(2.14.5)不满足要求 这些类别中的任何一个,因此是不可接受的 模板参数。 [示例:
template<class T, const char* p> class X { / ... / };
X<int, "Studebaker"> x1; // error: string literal as template-argument
const char p[] = "Vivisectionist";X<int,p> x2; // OK—结束示例]—结束注释]
那么为什么下面的代码在所有编译器(gcc 4.7.2、MSVC-11.0、Comeau)中都会出现错误?
template <const char* str>
void foo() {}
int main()
{
const char str[] = "str";
foo<str>();
}
【问题讨论】:
-
+1 它曾经适用于 MSCV 6 或 7 之类的东西。但上次我尝试它不再编译 :-( 很高兴你问这个问题。
标签: c++