我真的不明白为什么下面的代码不能编译:
template<const char*>
struct Foo{};
constexpr const char s1[] = "test1";
constexpr const char* const s2 = "test2";
int main()
{
Foo<s1> foo1; // ok
// Foo<s2> foo2; // doesn't compile
}
Run Code Online (Sandbox Code Playgroud)
取消注释最后一main()行使g ++和clang ++发出错误
Run Code Online (Sandbox Code Playgroud)error: 's2' is not a valid template argument because 's2' is a variable, not the address of a variable
和
Run Code Online (Sandbox Code Playgroud)error: non-type template argument for template parameter of pointer type 'const char *' must have its address taken
分别.
我的问题是:
s1实例化 …