Reputation: 3819
In C++ the following is legal:
template <int i>
run(){...}
run<3>(); // legal
const int j=3;
run<j>(); // legal because j is const
why the following are or aren't legal?
template <String s>
run(){...}
run<"hello">(); // legal or illegal?
const string s="hello";
run<s>(); // legal or illegal?
Upvotes: 0
Views: 79
Reputation: 206617
From the C++11 Standard:
14.1 Template parameters
...
4 A non-type template-parameter shall have one of the following (optionally cv-qualified) types:
— integral or enumeration type,
— pointer to object or pointer to function,
— lvalue reference to object or lvalue reference to function,
— pointer to member,
—std::nullptr_t
.
Hence, you cannot use a class as a non-type template parameter.
Upvotes: 6
Reputation: 4468
The int
one is legal because the compiler knows about int
. On the other hand, String
is not a built-in C++ data type. It is defined in a header and library. The constant "hello" is an array of characters, not a String()
.
Upvotes: 1