Стандарт C++ 11 (1119564), страница 94
Текст из файла (страница 94)
— endnote ]7A friend template shall not be declared in a local class.8Friend declarations shall not declare partial specializations. [ Example:§ 14.5.4342© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)template<class T> class A { };class X {template<class T> friend class A<T*>; // error};— end example ]9When a friend declaration refers to a specialization of a function template, the function parameter declarations shall not include default arguments, nor shall the inline specifier be used in such a declaration.14.5.5Class template partial specializations[temp.class.spec]1A primary class template declaration is one in which the class template name is an identifier. A templatedeclaration in which the class template name is a simple-template-id is a partial specialization of the classtemplate named in the simple-template-id.
A partial specialization of a class template provides an alternativedefinition of the template that is used instead of the primary definition when the arguments in a specializationmatch those given in the partial specialization (14.5.5.1). The primary template shall be declared beforeany specializations of that template. A partial specialization shall be declared before the first use of a classtemplate specialization that would make use of the partial specialization as the result of an implicit orexplicit instantiation in every translation unit in which such a use occurs; no diagnostic is required.2Each class template partial specialization is a distinct template and definitions shall be provided for themembers of a template partial specialization (14.5.5.3).3[ Example:template<classtemplate<classtemplate<classtemplate<classtemplate<classT1, class T2, int I>T, int I>T1, class T2, int I>T>T1, class T2, int I>classclassclassclassclassAA<T, T*, I>A<T1*, T2, I>A<int, T*, 5>A<T1, T2*, I>{{{{{};};};};};//////////#1#2#3#4#5The first declaration declares the primary (unspecialized) class template.
The second and subsequent declarations declare partial specializations of the primary template. — end example ]4The template parameters are specified in the angle bracket enclosed list that immediately follows the keywordtemplate. For partial specializations, the template argument list is explicitly written immediately followingthe class template name. For primary templates, this list is implicitly described by the template parameterlist. Specifically, the order of the template arguments is the sequence in which they appear in the templateparameter list. [ Example: the template argument list for the primary template in the example above is <T1,T2, I>. — end example ] [ Note: The template argument list shall not be specified in the primary templatedeclaration.
For example,template<class T1, class T2, int I> class A<T1, T2, I>{ };// error— end note ]5A class template partial specialization may be declared or redeclared in any namespace scope in which itsdefinition may be defined (14.5.1 and 14.5.2). [ Example:template<class T> struct A {struct C {template<class T2> struct B { };};};§ 14.5.5© ISO/IEC 2011 – All rights reserved343ISO/IEC 14882:2011(E)// partial specialization of A<T>::C::B<T2>template<class T> template<class T2>struct A<T>::C::B<T2*> { };A<short>::C::B<int*> absip;// uses partial specialization— end example ]6Partial specialization declarations themselves are not found by name lookup.
Rather, when the primarytemplate name is used, any previously-declared partial specializations of the primary template are alsoconsidered. One consequence is that a using-declaration which refers to a class template does not restrictthe set of partial specializations which may be found through the using-declaration. [ Example:namespace N {template<class T1, class T2> class A { };}using N::A;// primary template// refers to the primary templatenamespace N {template<class T> class A<T, T*> { }; // partial specialization}A<int,int*> a;// uses the partial specialization, which is found through// the using declaration which refers to the primary template— end example ]7A non-type argument is non-specialized if it is the name of a non-type parameter.
All other non-typearguments are specialized.8Within the argument list of a class template partial specialization, the following restrictions apply:— A partially specialized non-type argument expression shall not involve a template parameter of thepartial specialization except when the argument expression is a simple identifier. [ Example:template <int I, int J> struct A {};template <int I> struct A<I+5, I*2> {}; // errortemplate <int I, int J> struct B {};template <int I> struct B<I, I> {};// OK— end example ]— The type of a template parameter corresponding to a specialized non-type argument shall not bedependent on a parameter of the specialization. [ Example:template <class T, T t> struct C {};template <class T> struct C<T, 1>;// errortemplate< int X, int (*array_ptr)[X] > class A {};int array[5];template< int X > class A<X,&array> { };// error— end example ]— The argument list of the specialization shall not be identical to the implicit argument list of the primarytemplate.§ 14.5.5344© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— The template parameter list of a specialization shall not contain default template argument values.140— An argument shall not contain an unexpanded parameter pack.
If an argument is a pack expansion (14.5.3), it shall be the last argument in the template argument list.14.5.5.11Matching of class template partial specializations[temp.class.spec.match]When a class template is used in a context that requires an instantiation of the class, it is necessary todetermine whether the instantiation is to be generated using the primary template or one of the partialspecializations.
This is done by matching the template arguments of the class template specialization withthe template argument lists of the partial specializations.— If exactly one matching specialization is found, the instantiation is generated from that specialization.— If more than one matching specialization is found, the partial order rules (14.5.5.2) are used to determine whether one of the specializations is more specialized than the others. If none of the specializationsis more specialized than all of the other matching specializations, then the use of the class template isambiguous and the program is ill-formed.— If no matches are found, the instantiation is generated from the primary template.2A partial specialization matches a given actual template argument list if the template arguments of thepartial specialization can be deduced from the actual template argument list (14.8.2).
[ Example:A<int, int, 1>A<int, int*, 1>A<int, char*, 5>A<int, char*, 1>A<int*, int*, 2>a1;a2;a3;a4;a5;//////////uses #1uses #2, T is int, I is 1uses #4, T is charuses #5, T1 is int, T2 is char, I is 1ambiguous: matches #3 and #5— end example ]3A non-type template argument can also be deduced from the value of an actual template argument of anon-type parameter of the primary template.
[ Example: the declaration of a2 above. — end example ]4In a type name that refers to a class template specialization, (e.g., A<int, int, 1>) the argument list shallmatch the template parameter list of the primary template. The template arguments of a specialization arededuced from the arguments of the primary template.14.5.5.21Partial ordering of class template specializations[temp.class.order]For two class template partial specializations, the first is at least as specialized as the second if, given thefollowing rewrite to two function templates, the first function template is at least as specialized as the secondaccording to the ordering rules for function templates (14.5.6.2):— the first function template has the same template parameters as the first partial specialization and hasa single function parameter whose type is a class template specialization with the template argumentsof the first partial specialization, and— the second function template has the same template parameters as the second partial specializationand has a single function parameter whose type is a class template specialization with the templatearguments of the second partial specialization.2[ Example:140) There is no way in which they could be used.§ 14.5.5.2© ISO/IEC 2011 – All rights reserved345ISO/IEC 14882:2011(E)template<int I, int J, class T> class X { };template<int I, int J>class X<I, J, int> { }; // #1template<int I>class X<I, I, int> { }; // #2template<int I, int J> void f(X<I, J, int>);template<int I>void f(X<I, I, int>);// A// BThe partial specialization #2 is more specialized than the partial specialization #1 because the functiontemplate B is more specialized than the function template A according to the ordering rules for functiontemplates.
— end example ]14.5.5.31Members of class template specializations[temp.class.spec.mfunc]The template parameter list of a member of a class template partial specialization shall match the templateparameter list of the class template partial specialization. The template argument list of a member of a classtemplate partial specialization shall match the template argument list of the class template partial specialization.
A class template specialization is a distinct template. The members of the class template partialspecialization are unrelated to the members of the primary template. Class template partial specializationmembers that are used in a way that requires a definition shall be defined; the definitions of members of theprimary template are never used as definitions for members of a class template partial specialization. Anexplicit specialization of a member of a class template partial specialization is declared in the same way asan explicit specialization of the primary template. [ Example:// primary templatetemplate<class T, int I> struct A {void f();};template<class T, int I> void A<T,I>::f() { }// class template partial specializationtemplate<class T> struct A<T,2> {void f();void g();void h();};// member of class template partial specializationtemplate<class T> void A<T,2>::g() { }// explicit specializationtemplate<> void A<char,2>::h() { }int main() {A<char,0> a0;A<char,2> a2;a0.f();a2.g();a2.h();a2.f();//////////////OK, uses definition of primary template’s memberOK, uses definition ofpartial specialization’s memberOK, uses definition ofexplicit specialization’s memberill-formed, no definition of f for A<T,2>the primary template is not used here}§ 14.5.5.3346© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— end example ]2If a member template of a class template is partially specialized, the member template partial specializationsare member templates of the enclosing class template; if the enclosing class template is instantiated (14.7.1,14.7.2), a declaration for every member template partial specialization is also instantiated as part of creatingthe members of the class template specialization.