Page 724 , Chapter 25, The C++ Programming Language
A pointer used as a template argument must be of the form &of
, where of is the name of an object or a function, or of the form f
, where f
is the name of a function. A pointer to member must be of the form &X::of
, where of is the name of a member. In particular, a string literal is not acceptable as a template argument:
template<typename T, char∗ label>
class X {
// ...
};
X<int,"BMW323Ci"> x1; // **error : string literal as template argument**
char lx2[] = "BMW323Ci";
X<int,lx2> x2; // OK: lx2 has exter nal linkage
Page 725 , Chapter 25, The C++ Programming Language
This becomes particularly useful when combined with a default template argument (§25.2.5); for example:
template<typename T, T default_value = T{}>
class Vec {
// ...
};
Vec<int,42> c1;
Vec<int> c11; // default_value is int{}, that is, 0
Vec<string,"fortytwo"> c2; // **I'm confused!**
Vec<string> c22; // default_value is string{}; that is, ""