Стандарт C++ 98 (1119566), страница 66
Текст из файла (страница 66)
An exported template need only be declared (and not necessarily defined) in a translation unit inwhich it is instantiated. A template function declared both exported and inline is just inline and notexported.9[Note: an implementation may require that a translation unit containing the definition of an exported template be compiled before any translation unit containing an instantiation of that template.
]14.1 Template parameters1[temp.param]The syntax for template-parameters is:template-parameter:type-parameterparameter-declarationtype-parameter:class identifieroptclass identifieropt = type-idtypename identifieropttypename identifieropt = type-idtemplate < template-parameter-list > class identifieropttemplate < template-parameter-list > class identifieropt = id-expression2There is no semantic difference between class and typename in a template-parameter.
typenamefollowed by an unqualified-id names a template type parameter. typename followed by a qualified-namedenotes the type in a non-type 126) parameter-declaration. A storage class shall not be specified in atemplate-parameter declaration. [Note: a template parameter may be a class template. For example,template<class T> class myarray { /* ... */ };template<class K, class V, template<class T> class C = myarray>class Map {C<K> key;C<V> value;// ...};—end note]3A type-parameter defines its identifier to be a type-name (if declared with class or typename) ortemplate-name (if declared with template) in the scope of the template declaration.
[Note: because ofthe name lookup rules, a template-parameter that could be interpreted as either a non-type templateparameter or a type-parameter (because its identifier is the name of an already existing class) is taken as atype-parameter. For example,class T { /* ... */ };int i;template<class T, T i> void f(T t){T t1 = i;// template-parameters T and i::T t2 = ::i;// global namespace members T and i}Here, the template f has a type-parameter called T, rather than an unnamed non-type template-parameterof class T.
]__________________126) Since template template-parameters and template template-arguments are treated as types for descriptive purposes, the termsnon-type parameter and non-type argument are used to refer to non-type, non-template parameters and arguments.2364© ISO/IECISO/IEC 14882:1998(E)14 Templates14.1 Template parametersA 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,— reference to object or reference to function,— pointer to member.5[Note: other types are disallowed either explicitly below or implicitly by the rules governing the form oftemplate-arguments (14.3).
] The top-level cv-qualifiers on the template-parameter are ignored whendetermining its type.6A non-type non-reference template-parameter is not an lvalue. It shall not be assigned to or in any otherway have its value changed. A non-type non-reference template-parameter cannot have its address taken.When a non-type non-reference template-parameter is used as an initializer for a reference, a temporary isalways used. [Example:template<const X& x, int i> void f(){i++;// error: change of template-parameter value&x;&i;// OK// error: address of non-reference template-parameterint& ri = i;const int& cri = i;// error: non-const reference bound to temporary// OK: const reference bound to temporary}—end example]7A non-type template-parameter shall not be declared to have floating point, class, or void type. [Example:template<double d> class X;template<double* pd> class Y;template<double& rd> class Z;// error// OK// OK—end example]8A non-type template-parameter of type “array of T” or “function returning T” is adjusted to be of type“pointer to T” or “pointer to function returning T”, respectively.
[Example:template<int *a>struct R { /* ... */ };template<int b[5]> struct S { /* ... */ };int *p;R<p> w;// OKS<p> x;// OK due to parameter adjustmentint v[5];R<v> y;// OK due to implicit argument conversionS<v> z;// OK due to both adjustment and conversion—end example]9A default template-argument is a template-argument (14.3) specified after = in a template-parameter.
Adefault template-argument may be specified for any kind of template-parameter (type, non-type, template).A default template-argument may be specified in a class template declaration or a class template definition.A default template-argument shall not be specified in a function template declaration or a function templatedefinition, nor in the template-parameter-list of the definition of a member of a class template.10The set of default template-arguments available for use with a template declaration or definition is obtainedby merging the default arguments from the definition (if in scope) and all declarations in scope in the sameway default function arguments are (8.3.6).
[Example:237ISO/IEC 14882:1998(E)© ISO/IEC14.1 Template parameters14 Templatestemplate<class T1, class T2 = int> class A;template<class T1 = int, class T2> class A;is equivalent totemplate<class T1 = int, class T2 = int> class A;—end example]11If a template-parameter has a default template-argument, all subsequent template-parameters shall have adefault template-argument supplied.
[Example:template<class T1 = int, class T2> class B;// error—end example]12A template-parameter may not be given default arguments by two different declarations in the same scope.[Example:template<class T = int> class X;template<class T = int> class X { /*... */ }; // error—end example]13The scope of a template-parameter extends from its point of declaration until the end of its template. In particular, a template-parameter can be used in the declaration of subsequent template-parameters and theirdefault arguments.
[Example:template<class T, T* p, class U = T> class X { /* ... */ };template<class T> void f(T* p = new T);—end example]14A template-parameter cannot be used in preceding template-parameters or their default arguments.15When parsing a default template-argument for a non-type template-parameter, the first non-nested > istaken as the end of the template-parameter-list rather than a greater-than operator.
[Example:template<int i = 3 > 4 >class X { /* ... */ };// syntax errortemplate<int i = (3 > 4) >class Y { /* ... */ };// OK—end example]14.2 Names of template specializations1[temp.names]A template specialization (14.7) can be referred to by a template-id:template-id:template-name < template-argument-listopt >template-name:identifiertemplate-argument-list:template-argumenttemplate-argument-list , template-argumenttemplate-argument:assignment-expressiontype-idid-expression[Note: the name lookup rules (3.4) are used to associate the use of a name with a template declaration; that238© ISO/IECISO/IEC 14882:1998(E)14 Templates14.2 Names of template specializationsis, to identify a name as a template-name.
]2For a template-name to be explicitly qualified by the template arguments, the name must be known to referto a template.3After name lookup (3.4) finds that a name is a template-name, if this name is followed by a <, the < isalways taken as the beginning of a template-argument-list and never as a name followed by the less-thanoperator. When parsing a template-id, the first non-nested >127) is taken as the end of the templateargument-list rather than a greater-than operator. [Example:template<int i> class X { /* ... */ };X< 1>2 >X<(1>2)>x1;x2;// syntax error// OKtemplate<class T> class Y { /* ... */ };Y< X<1> >x3;// OKY<X<6>> 1> >x4;// OK: Y< X< (6>>1) > >—end example]4When the name of a member template specialization appears after .
or -> in a postfix-expression, or afternested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on atemplate-parameter (14.6.2), the member template name must be prefixed by the keyword template.Otherwise the name is assumed to name a non-template. [Example:class X {public:template<size_t> X* alloc();template<size_t> static X* adjust();};template<class T> void f(T* p){T* p1 = p->alloc<200>();// ill-formed: < means less thanT* p2 = p->template alloc<200>();// OK: < starts template argument listT::adjust<100>();// ill-formed: < means less thanT::template adjust<100>();// OK: < starts explicit qualification}—end example]5If a name prefixed by the keyword template is not the name of a member template, the program is illformed.
[Note: the keyword template may not be applied to non-template members of class templates.]6A template-id that names a class template specialization is a class-name (clause 9).14.3 Template arguments1[temp.arg]There are three forms of template-argument, corresponding to the three forms of template-parameter: type,non-type and template. The type and form of each template-argument specified in a template-id shallmatch the type and form specified for the corresponding parameter declared by the template in its__________________127) A > that encloses the type-id of a dynamic_cast, static_cast, reinterpret_cast or const_cast, or whichencloses the template-arguments of a subsequent template-id, is considered nested for the purpose of this description.239ISO/IEC 14882:1998(E)© ISO/IEC14.3 Template arguments14 Templatestemplate-parameter-list.