Есть шаблонный класс A
и из него я хочу сделать 2 конкретных: B
и 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;
}
но получаю ошибку:
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();
^~~
Если у A
в шаблоне вместо строки целое число, то код прекрасно работает.