Ru-Brd |
Note that nontype template parameters carry some restrictions. In general, they may be constant integral values (including enumerations) or pointers to objects with external linkage. Floating-point numbers and class-type objects are not allowed as nontype template parameters: template <double VAT> // ERROR: floating-point values are not double process (double v) // allowed as template parameters { return v * VAT; } template <std::string name> // ERROR: class-type objects are not class MyClass { // allowed as template parameters }; Not being able to use floating-point literals (and simple constant floating-point expressions) as template arguments has historical reasons. Because there are no serious technical challenges, this may be supported in future versions of C++ (see Section 13.4 on page 210). Because string literals are objects with internal linkage (two string literals with the same value but in different modules are different objects), you can't use them as template arguments either: template <char const* name> class MyClass { }; MyClass<"hello"> x; // ERROR: string literal "hello" not allowed You cannot use a global pointer either: template <char const* name> class MyClass { }; char const* s = "hello"; MyClass<s> x; // ERROR: s is pointer to object with internal linkage However, the following is possible: template <char const* name> class MyClass { }; extern char const s[] = "hello"; MyClass<s> x; // OK The global character array s is initialized by "hello" so that s is an object with external linkage. See Section 8.3.3 on page 109 for a detailed discussion and Section 13.4 on page 209 for a discussion of possible future changes in this area. |
Ru-Brd |