Стандарт C++ 11 (1119564), страница 89
Текст из файла (страница 89)
[ Note: As with all thesedescriptions of candidate functions, this declaration serves only to describe the built-in operator for purposesof overload resolution. The operator “?:” cannot be overloaded. — end note ]25For every type T , where T is a pointer, pointer-to-member, or scoped enumeration type, there exist candidateoperator functions of the formToperator?:(bool, T , T );§ 13.6320© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)141Templates[temp]A template defines a family of classes or functions or an alias for a family of types.template-declaration:template < template-parameter-list > declarationtemplate-parameter-list:template-parametertemplate-parameter-list , template-parameter[ Note: The > token following the template-parameter-list of a template-declaration may be the product ofreplacing a >> token by two consecutive > tokens (14.2).
— end note ]The declaration in a template-declaration shall— declare or define a function or a class, or— define a member function, a member class, a member enumeration, or a static data member of a classtemplate or of a class nested within a class template, or— define a member template of a class or class template, or— be an alias-declaration.A template-declaration is a declaration. A template-declaration is also a definition if its declaration definesa function, a class, or a static data member.2A template-declaration can appear only as a namespace scope or class scope declaration.
In a functiontemplate declaration, the last component of the declarator-id shall not be a template-id. [ Note: That lastcomponent may be an identifier, an operator-function-id, a conversion-function-id, or a literal-operator-id.In a class template declaration, if the class name is a simple-template-id, the declaration declares a classtemplate partial specialization (14.5.5). — end note ]3In a template-declaration, explicit specialization, or explicit instantiation the init-declarator-list in the declaration shall contain at most one declarator. When such a declaration is used to declare a class template,no declarator is permitted.4A template name has linkage (3.5).
A non-member function template can have internal linkage; any othertemplate name shall have external linkage. Specializations (explicit or implicit) of a template that hasinternal linkage are distinct from all specializations in other translation units. A template, a template explicit specialization (14.7.3), and a class template partial specialization shall not have C linkage. Use ofa linkage specification other than C or C++ with any of these constructs is conditionally-supported, withimplementation-defined semantics. Template definitions shall obey the one definition rule (3.2).
[ Note: Default arguments for function templates and for member functions of class templates are considered definitionsfor the purpose of template instantiation (14.5) and must also obey the one definition rule. — end note ]5A class template shall not have the same name as any other template, class, function, variable, enumeration,enumerator, namespace, or type in the same scope (3.3), except as specified in (14.5.5).
Except that afunction template can be overloaded either by (non-template) functions with the same name or by otherfunction templates with the same name (14.8.3), a template name declared in namespace scope or in classscope shall be unique in that scope.© ISO/IEC 2011 – All rights reserved321ISO/IEC 14882:2011(E)6A function template, member function of a class template, or static data member of a class template shallbe defined in every translation unit in which it is implicitly instantiated (14.7.1) unless the correspondingspecialization is explicitly instantiated (14.7.2) in some translation unit; no diagnostic is required.14.11Template parameters[temp.param]The syntax for template-parameters is:template-parameter:type-parameterparameter-declarationtype-parameter:class ...opt identifieroptclass identifieropt = type-idtypename ...opt identifieropttypename identifieropt = type-idtemplate < template-parameter-list > class ...opt identifieropttemplate < template-parameter-list > class identifieropt = id-expression[ Note: The > token following the template-parameter-list of a type-parameter may be the product of replacinga >> token by two consecutive > tokens (14.2).
— end note ]2There is no semantic difference between class and typename in a template-parameter. typename followedby an unqualified-id names a template type parameter. typename followed by a qualified-id denotes thetype in a non-type 137 parameter-declaration. A storage class shall not be specified in a template-parameterdeclaration. [ 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 whose identifier does not follow an ellipsis defines its identifier to be a typedef-name (ifdeclared with class or typename) or template-name (if declared with template) in the scope of the templatedeclaration. [ Note: Because of the name lookup rules, a template-parameter that could be interpreted aseither a non-type template-parameter or a type-parameter (because its identifier is the name of an alreadyexisting class) is taken as a type-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.
— end note ]137) Since template template-parameters and template template-arguments are treated as types for descriptive purposes, theterms non-type parameter and non-type argument are used to refer to non-type, non-template parameters and arguments.§ 14.1322© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)4A 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.5[ Note: Other types are disallowed either explicitly below or implicitly by the rules governing the form oftemplate-arguments (14.3). — end note ] The top-level cv-qualifiers on the template-parameter are ignoredwhen determining its type.6A non-type non-reference template-parameter is a prvalue.
It shall not be assigned to or in any other wayhave its value changed. A non-type non-reference template-parameter cannot have its address taken. Whena non-type non-reference template-parameter is used as an initializer for a reference, a temporary is alwaysused. [ 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;// OK// OK due to parameter adjustmentS<&p> x;int 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.
A default template-argument may be specified for any kind of template-parameter (type, non-type, template) thatis not a template parameter pack (14.5.3). A default template-argument may be specified in a template declaration. A default template-argument shall not be specified in the template-parameter-lists of the definition§ 14.1© ISO/IEC 2011 – All rights reserved323ISO/IEC 14882:2011(E)of a member of a class template that appears outside of the member’s class.
A default template-argumentshall not be specified in a friend class template declaration. If a friend function template declaration specifiesa default template-argument, that declaration shall be a definition and shall be the only declaration of thefunction template in the translation unit.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:template<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 of a class template or alias template has a default template-argument, each subsequent template-parameter shall either have a default template-argument supplied or be a template parameterpack.
If a template-parameter of a primary class template or alias template is a template parameter pack,it shall be the last template-parameter. A template parameter pack of a function template shall not befollowed by another template parameter unless that template parameter can be deduced or has a defaultargument (14.8.2). [ Example:template<class T1 = int, class T2> class B;// error// U cannot be deduced or specifiedtemplate<class...