There is a template class A and from it I want to make 2 specific ones: B and C
#include <iostream> using namespace std; template<const char* s = "hello"> class A { public: void foo(); }; template<const char* s> void A<s>::foo() { cout << s; } typedef A<" world"> B; class C : public A<"!\n"> {}; int main() { A<"hello"> a; B b; C c; a.foo(); b.foo(); c.foo(); return 0; } but I get the error:
prog.cpp:15:19: error: '" world"' is not a valid template argument for type 'const char*' because string literals can never be used in this context typedef A<" world"> B; ^ prog.cpp:17:25: error: '"!\012"' is not a valid template argument for type 'const char*' because string literals can never be used in this context class C : public A<"!\n"> {}; ^ prog.cpp: In function 'int main()': prog.cpp:20:11: error: '"hello"' is not a valid template argument for type 'const char*' because string literals can never be used in this context A<"hello"> a; ^ prog.cpp:23:4: error: request for member 'foo' in 'a', which is of non-class type 'int' a.foo(); ^~~ prog.cpp:24:4: error: request for member 'foo' in 'b', which is of non-class type 'B {aka int}' b.foo(); ^~~ prog.cpp:25:4: error: 'class C' has no member named 'foo' c.foo(); ^~~ If A an integer in the pattern instead of a string, then the code works great.