我知道C ++禁止使用字符串文字实例化模板而不是const char*
,但我不明白为什么。仅限char
s或const
数组的全局变量数组。是什么原因或打破用例禁止像A<"A">
这样最方便的用法? Live example
template<const char* S>
class A
{};
char a[] = "A";
const char* p = "A";
int main()
{
A<"A"> a1; // error: '"A"' is not a valid template argument for type 'const char*' because string literals can never be used in this context
A<p> a2; // error: 'p' is not a valid template argument because 'p' is a variable, not the address of a variable
A<a> a3; // ok
}