Стандарт C++ 11 (1119564), страница 92
Текст из файла (страница 92)
— end example ]2If an expression e involves a template parameter, decltype(e) denotes a unique dependent type. Two suchdecltype-specifiers refer to the same type only if their expressions are equivalent (14.5.6.1). [ Note: however,it may be aliased, e.g., by a typedef-name. — end note ]14.51Template declarations[temp.decls]A template-id, that is, the template-name followed by a template-argument-list shall not be specified in thedeclaration of a primary template declaration. [ Example:template<class T1, class T2, int I> class A<T1, T2, I> { };template<class T1, int I> void sort<T1, I>(T1 data[I]);// error// error— end example ] [ Note: However, this syntax is allowed in class template partial specializations (14.5.5).— end note ]2For purposes of name lookup and instantiation, default arguments of function templates and default arguments of member functions of class templates are considered definitions; each default argument is a separatedefinition which is unrelated to the function template definition or to any other default arguments.3Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specializean alias template.14.5.11Class templates[temp.class]A class template defines the layout and operations for an unbounded set of related types.
[ Example: a singleclass template List might provide a common definition for list of int, list of float, and list of pointers toShapes. — end example ][ Example: An array class template might be declared like this:§ 14.5.1334© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)template<class T> class Array {T* v;int sz;public:explicit Array(int);T& operator[](int);T& elem(int i) { return v[i]; }};2The prefix template <class T> specifies that a template is being declared and that a type-name T will beused in the declaration. In other words, Array is a parameterized type with T as its parameter. — endexample ]3When a member function, a member class, a member enumeration, a static data member or a membertemplate of a class template is defined outside of the class template definition, the member definition isdefined as a template definition in which the template-parameters are those of the class template.
Thenames of the template parameters used in the definition of the member may be different from the templateparameter names used in the class template definition. The template argument list following the classtemplate name in the member definition shall name the parameters in the same order as the one used in thetemplate parameter list of the member. Each template parameter pack shall be expanded with an ellipsisin the template argument list.
[ Example:template<class T1, class T2> struct A {void f1();void f2();};template<class T2, class T1> void A<T2,T1>::f1() { }template<class T2, class T1> void A<T1,T2>::f2() { }// OK// errortemplate<class ... Types> struct B {void f3();void f4();};template<class ...
Types> void B<Types ...>::f3() { }template<class ... Types> void B<Types>::f4() { }// OK// error— end example ]4In a redeclaration, partial specialization, explicit specialization or explicit instantiation of a class template,the class-key shall agree in kind with the original class template declaration (7.1.6.3).14.5.1.11Member functions of class templates[temp.mem.func]A member function of a class template may be defined outside of the class template definition in which it isdeclared.
[ Example:template<class T> class Array {T* v;int sz;public:explicit Array(int);T& operator[](int);T& elem(int i) { return v[i]; }};§ 14.5.1.1© ISO/IEC 2011 – All rights reserved335ISO/IEC 14882:2011(E)declares three function templates. The subscript function might be defined like this:template<class T> T& Array<T>::operator[](int i) {if (i<0 || sz<=i) error("Array: range error");return v[i];}— end example ]2The template-arguments for a member function of a class template are determined by the template-argumentsof the type of the object for which the member function is called.
[ Example: the template-argument forArray<T> :: operator [] () will be determined by the Array to which the subscripting operation is applied.Array<int> v1(20);Array<dcomplex> v2(30);v1[3] = 7;v2[3] = dcomplex(7,8);// Array<int>::operator[]()// Array<dcomplex>::operator[]()— end example ]14.5.1.21Member classes of class templates[temp.mem.class]A class member of a class template may be defined outside the class template definition in which it is declared.[ Note: The class member must be defined before its first use that requires an instantiation (14.7.1). Forexample,template<class T> struct A {class B;};A<int>::B* b1;// OK: requires A to be defined but not A::Btemplate<class T> class A<T>::B { };A<int>::B b2;// OK: requires A::B to be defined— end note ]14.5.1.31Static data members of class templates[temp.static]A definition for a static data member may be provided in a namespace scope enclosing the definition of thestatic member’s class template.
[ Example:template<class T> class X {static T s;};template<class T> T X<T>::s = 0;— end example ]2An explicit specialization of a static data member declared as an array of unknown bound can have a differentbound from its definition, if any. [ Example:templatestatic};templatetemplate<class T> struct A {int i[];<class T> int A<T>::i[4];<> int A<int>::i[] = { 1 };// 4 elements// OK: 1 element§ 14.5.1.3336© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— end example ]14.5.1.41Enumeration members of class templates[temp.mem.enum]An enumeration member of a class template may be defined outside the class template definition. [ Example:template<class T> struct A {enum E : T;};A<int> a;template<class T> enum A<T>::E : T { e1, e2 };A<int>::E e = A<int>::e1;— end example ]14.5.21Member templates[temp.mem]A template can be declared within a class or class template; such a template is called a member template.
Amember template can be defined within or outside its class definition or class template definition. A membertemplate of a class template that is defined outside of its class template definition shall be specified withthe template-parameters of the class template followed by the template-parameters of the member template.[ Example:template<class T> struct string {template<class T2> int compare(const T2&);template<class T2> string(const string<T2>& s) { /∗ ... ∗/ }};template<class T> template<class T2> int string<T>::compare(const T2& s) {}— end example ]2A local class shall not have member templates. Access control rules (Clause 11) apply to member templatenames.
A destructor shall not be a member template. A normal (non-template) member function with agiven name and type and a member function template of the same name, which could be used to generatea specialization of the same type, can both be declared in a class. When both exist, a use of that name andtype refers to the non-template member unless an explicit template argument list is supplied. [ Example:template <class T> struct A {void f(int);template <class T2> void f(T2);};template <> void A<int>::f(int) { }template <> template <> void A<int>::f<>(int) { }int main() {A<char> ac;ac.f(1);ac.f(’c’);ac.f<>(1);}// non-template member// template member// non-template// template// template— end example ]§ 14.5.2© ISO/IEC 2011 – All rights reserved337ISO/IEC 14882:2011(E)3A member function template shall not be virtual.
[ Example:template <class T> struct AA {template <class C> virtual void g(C);virtual void f();};// error// OK— end example ]4A specialization of a member function template does not override a virtual function from a base class.[ Example:class B {virtual void f(int);};class D : public B {template <class T> void f(T); // does not override B::f(int)void f(int i) { f<>(i); }// overriding function that calls// the template instantiation};— end example ]5A specialization of a conversion function template is referenced in the same way as a non-template conversionfunction that converts to the same type.
[ Example:struct A {template <class T> operator T*();};template <class T> A::operator T*(){ return 0; }template <> A::operator char*(){ return 0; }// specializationtemplate A::operator void*();// explicit instantiationint main() {A a;int *ip;ip = a.operator int*();// explicit call to template operator// A::operator int*()}— end example ] [ Note: Because the explicit template argument list follows the function template name,and because conversion member function templates and constructor member function templates are calledwithout using a function name, there is no way to provide an explicit template argument list for thesefunction templates. — end note ]6A specialization of a conversion function template is not found by name lookup. Instead, any conversionfunction templates visible in the context of the use are considered.
For each such operator, if argumentdeduction succeeds (14.8.2.3), the resulting specialization is used as if found by name lookup.7A using-declaration in a derived class cannot refer to a specialization of a conversion function template in abase class.§ 14.5.2338© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)8Overload resolution (13.3.3.2) and partial ordering (14.5.6.2) are used to select the best conversion functionamong multiple specializations of conversion function templates and/or non-template conversion functions.14.5.31Variadic templates[temp.variadic]A template parameter pack is a template parameter that accepts zero or more template arguments. [ Example:template<class ...