1

I know that C++ forbids instantiate template neither with string literal not with const char* but I don't understand why. Only global variable array of chars or const array. What is the reason or breaking use case to forbid most convenient usage like 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
}
Viktor
  • 1,004
  • 1
  • 10
  • 16

0 Answers0