Some code that I don't understand:
template<const char *T>
class A
{
};
const char *arr="hello world";
int main()
{
A<arr> obj;
}
This snippet won't compile.
Error message from visual studio compiler is :
Invalid template argument for 'A', expected compile-time constant expression
Error messega from g++ is:
'arr' is not a valid template argument because 'arr' is a variable , not the address of a variable
For visual studio compiler, even after I change the const
to constexpr
, that code still doesn't compile.
Why is that? Is that relevant to those internal linkage and external linkage thing?(I read this from the book C++ template, but don't understand why and how)
Additionally, change the const char *arr = "Hello world";
to
const char arr[] = "Hello world";
or
external const char *arr="Hello world";
won't work.
But this would work:external const char arr[]="Hello world";