Стандарт C++ 98 (1119566), страница 76
Текст из файла (страница 76)
If the declarationof an explicit specialization for such a member appears in namespace scope, the member declaration shallbe preceded by a template<> for each enclosing class template that is explicitly specialized. [Example:template<class T1> class A {template<class T2> class B {void mf();};};template<> template<> A<int>::B<double> { };template<> template<> void A<char>::B<char>::mf() { };—end example]18In an explicit specialization declaration for a member of a class template or a member template that appearsin namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing classtemplates are not explicitly specialized as well.
In such explicit specialization declaration, the keywordtemplate followed by a template-parameter-list shall be provided instead of the template<> preceding the explicit specialization declaration of the member. The types of the template-parameters in thetemplate-parameter-list shall be the same as those specified in the primary template definition. [Example:template<class T1> class A {template<class T2> class B {template<class T3> void mf1(T3);void mf2();};};template<> template<class X>class A<int>::B { };template<> template<> template<class T>void A<int>::B<double>::mf1(T t) { };template<class Y> template<>void A<Y>::B<double>::mf2() { }; // ill-formed; B<double> is specialized but// its enclosing class template A is not—end example]19A specialization of a member function template or member class template of a non-specialized class template is itself a template.20An explicit specialization declaration shall not be a friend declaration.21Default function arguments shall not be specified in a declaration or a definition for one of the followingexplicit specializations:— the explicit specialization of a function template;— the explicit specialization of a member function template;— the explicit specialization of a member function of a class template where the class template specialization to which the member function specialization belongs is implicitly instantiated.
[Note: default function arguments may be specified in the declaration or definition of a member function of a class template specialization that is explicitly specialized. ]14.8 Function template specializations1[temp.fct.spec]A function instantiated from a function template is called a function template specialization; so is anexplicit specialization of a function template. Template arguments can either be explicitly specified whennaming the function template specialization or be deduced (14.8.2) from the context, e.g.
from the functionarguments in a call to the function template specialization.277ISO/IEC 14882:1998(E)© ISO/IEC14.8 Function template specializations214 TemplatesEach function template specialization instantiated from a template has its own copy of any static variable.[Example:template<class T> void f(T* p){static T s;// ...};void g(int a, char* b){f(&a);f(&b);}// call f<int>(int*)// call f<char*>(char**)Here f<int>(int*) has a static variable s of type int and f<char*>(char**) has a static variables of type char*. ]14.8.1 Explicit template argument specification1[temp.arg.explicit]Template arguments can be specified when referring to a function template specialization by qualifying thefunction template name with the list of template-arguments in the same way as template-arguments arespecified in uses of a class template specialization.
[Example:template<class T> void sort(Array<T>& v);void f(Array<dcomplex>& cv, Array<int>& ci){sort<dcomplex>(cv);// sort(Array<dcomplex>&)sort<int>(ci);// sort(Array<int>&)}andtemplate<class U, class V> U convert(V v);void g(double d){int i = convert<int,double>(d);char c = convert<char,double>(d);}// int convert(double)// char convert(double)—end example]2A template argument list may be specified when referring to a specialization of a function template— when a function is called,— when the address of a function is taken, when a function initializes a reference to function, or when apointer to member function is formed,— in an explicit specialization,— in an explicit instantiation, or— in a friend declaration.Trailing template arguments that can be deduced (14.8.2) may be omitted from the list of explicit templatearguments.
If all of the template arguments can be deduced, they may all be omitted; in this case, theempty template argument list <> itself may also be omitted. [Example:278© ISO/IECISO/IEC 14882:1998(E)14 Templates14.8.1 Explicit template argument specificationtemplate<class X, class Y> X f(Y);void g(){int i = f<int>(5.6);// Y is deduced to be doubleint j = f(5.6);// ill-formed: X cannot be deduced}—end example] [Note: An empty template argument list can be used to indicate that a given use refers to aspecialization of a function template even when a normal (i.e., nontemplate) function is visible that wouldotherwise be used.
For example:template <class T> int f(T);int f(int);int k = f(1);int l = f<>(1);// #1// #2// uses #2// uses #1—end note]3Template arguments that are present shall be specified in the declaration order of their correspondingtemplate-parameters. The template argument list shall not specify more template-arguments than there arecorresponding template-parameters.
[Example:template<class X, class Y, class Z> X f(Y,Z);void g(){f<int,char*,double>("aa",3.0);f<int,char*>("aa",3.0); // Z is deduced to be doublef<int>("aa",3.0);// Y is deduced to be char*, and// Z is deduced to be doublef("aa",3.0);// error: X cannot be deduced}—end example]4Implicit conversions (clause 4) will be performed on a function argument to convert it to the type of thecorresponding function parameter if the parameter type contains no template-parameters that participate intemplate argument deduction. [Note: template parameters do not participate in template argument deduction if they are explicitly specified.
For example,template<class T> void f(T);class Complex {// ...Complex(double);};void g(){f<Complex>(1);}// OK, means f<Complex>(Complex(1))—end note]5[Note: because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using afunction name, there is no way to provide an explicit template argument list for these function templates.
]6[Note: For simple function names, argument dependent lookup (3.4.2) applies even when the function nameis not visible within the scope of the call. This is because the call still has the syntactic form of a functioncall (3.4.1). But when a function template with explicit template arguments is used, the call does not havethe correct syntactic form unless there is a function template with that name visible at the point of the call.If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup doesnot apply.
If some such name is visible, argument dependent lookup applies and additional function279ISO/IEC 14882:1998(E)© ISO/IEC14.8.1 Explicit template argument specification14 Templatestemplates may be found in other namespaces. [Example:namespace A {struct B { };template<int X> void f();}namespace C {template<class T> void f(T t);}void g(A::B b) {f<3>(b);// ill-formed: not a function callA::f<3>(b);// well-formedC::f<3>(b);// ill-formed; argument dependent lookup// only applies to unqualified namesusing C::f;f<3>(b);// well-formed because C::f is visible; then// A::f is found by argument dependent lookup}—end example] —end note]14.8.2 Template argument deduction1[temp.deduct]When a template function specialization is referenced, all of the template arguments must have values.
Thevalues can be either explicitly specified or, in some cases, deduced from the use. [Example:void f(Array<dcomplex>& cv, Array<int>& ci){sort(cv);// call sort(Array<dcomplex>&)sort(ci);// call sort(Array<int>&)}andvoid g(double d){int i = convert<int>(d);int c = convert<char>(d);}// call convert<int,double>(double)// call convert<char,double>(double)—end example]2When an explicit template argument list is specified, the template arguments must be compatible with thetemplate parameter list and must result in a valid function type as described below; otherwise type deduction fails.
Specifically, the following steps are performed when evaluating an explicitly specified templateargument list with respect to a given function template:— The specified template arguments must match the template parameters in kind (i.e., type, nontype, template), and there must not be more arguments than there are parameters; otherwise type deduction fails.— Nontype arguments must match the types of the corresponding nontype template parameters, or must beconvertible to the types of the corresponding nontype parameters as specified in 14.3.2, otherwise typededuction fails.— All references in the function type of the function template to the corresponding template parameters arereplaced by the specified template argument values.