You can't use string literals as a template argument, for the
simple reason that it's unspecified whether two instances of a
literal with the same text are the same object or not. In other
words, given:
template
class TC {};
TC< "xyz" > v1;
TC< "xyz" > v2;
It would be unspecified whether v1
and v2
had the same type
or not.
You can use char const[]
variables as template arguments,
however, since they have a defined address:
template
class TC {};
extern char const xyz[] = "xyz";
TC< xyz > v1;
TC< xyz > v2;
In this case, v1
and v2
are guaranteed to have the same
type.
EDIT:
I think C++11 removes the need for the extern
on the
definition of the string, at least if the string and the
instantiation are all in the same translation unit. I'm not
sure, however; the one time I did something like this, I didn't
have access to C++11.