Стандарт C++ 98 (1119566), страница 67
Текст из файла (страница 67)
[Example:template<class T> class Array {T* v;int sz;public:explicit Array(int);T& operator[](int);T& elem(int i) { return v[i]; }// ...};Array<int> v1(20);typedef complex<double> dcomplex;// complex is a standard// library templateArray<dcomplex> v2(30);Array<dcomplex> v3(40);void bar() {v1[3] = 7;v2[3] = v3.elem(4) = dcomplex(7,8);}—end example]2In a template-argument, an ambiguity between a type-id and an expression is resolved to a type-id, regardless of the form of the corresponding template-parameter.128) [Example:template<class T> void f();template<int I> void f();void g(){f<int()>();}// int() is a type-id: call the first f()—end example]3The name of a template-argument shall be accessible at the point where it is used as a template-argument.[Note: if the name of the template-argument is accessible at the point where it is used as a templateargument, there is no further access restriction in the resulting instantiation where the correspondingtemplate-parameter name is used.
] [Example:template<class T> class X {static T t;};class Y {private:struct S { /* ... */ };X<S> x;// OK: S is accessible// X<Y::S> has a static member of type Y::S// OK: even though Y::S is private};X<Y::S> y;// error: S not accessible—end example] For a template-argument of class type, the template definition has no special access rightsto the inaccessible members of the template argument type.__________________128) There is no such ambiguity in a default template-argument because the form of the template-parameter determines the allowableforms of the template-argument.240© ISO/IECISO/IEC 14882:1998(E)14 Templates414.3 Template argumentsWhen default template-arguments are used, a template-argument list can be empty.
In that case the empty<> brackets shall still be used as the template-argument-list. [Example:template<class T = char> class String;String<>* p;// OK: String<char>String* q;// syntax error—end example]5An explicit destructor call (12.4) for an object that has a type that is a class template specialization mayexplicitly specify the template-arguments. [Example:template<class T> struct A {~A();};void f(A<int>* p, A<int>* q) {p->A<int>::~A();q->A<int>::~A<int>();}// OK: destructor call// OK: destructor call—end example]6If the use of a template-argument gives rise to an ill-formed construct in the instantiation of a template specialization, the program is ill-formed.7When the template in a template-id is an overloaded function template, both non-template functions in theoverload set and function templates in the overload set for which the template-arguments do not match thetemplate-parameters are ignored.
If none of the function templates have matching template-parameters,the program is ill-formed.14.3.1 Template type arguments[temp.arg.type]1A template-argument for a template-parameter which is a type shall be a type-id.2A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shallnot be used as a template-argument for a template type-parameter. [Example:template <class T> class X { /* ... */ };void f(){struct S { /* ...
*/ };X<S> x3;X<S*> x4;// error: local type used as template-argument// error: pointer to local type used as template-argument}—end example] [Note: a template type argument may be an incomplete type (3.9). ]3If a declaration acquires a function type through a type dependent on a template-parameter and this causesa declaration that does not use the syntactic form of a function declarator to have function type, the programis ill-formed. [Example:template<class T> struct A {static T t;};typedef int function();A<function> a;// ill-formed: would declare A<function>::t// as a static member function—end example]241ISO/IEC 14882:1998(E)© ISO/IEC14.3.2 Template non-type arguments14 Templates14.3.2 Template non-type arguments1[temp.arg.nontype]A template-argument for a non-type, non-template template-parameter shall be one of:— an integral constant-expression of integral or enumeration type; or— the name of a non-type template-parameter; or— the name of an object or function with external linkage, including function templates and functiontemplate-ids but excluding non-static class members, expressed as id-expression; or— the address of an object or function with external linkage, including function templates and functiontemplate-ids but excluding non-static class members, expressed as & id-expression where the & isoptional if the name refers to a function or array; or— a pointer to member expressed as described in 5.3.1 .2[Note: A string literal (2.13.4) is not an acceptable template-argument because a string literal is an objectwith internal linkage.
[Example:template<class T, char* p> class X {// ...X();X(const char* q) { /* ... */ }};X<int,"Studebaker"> x1;// error: string literal as template-argumentchar p[] = "Vivisectionist";X<int,p> x2;// OK—end example] —end note]3[Note: Addresses of array elements and names or addresses of non-static class members are not acceptabletemplate-arguments. [Example:template<int* p> class X { };int a[10];struct S { int m; static int s; } s;X<&a[2]> x3;X<&s.m> x4;X<&s.s> x5;X<&S::s> x6;// error: address of array element// error: address of non-static member// error: &S::s must be used// OK: address of static member—end example] —end note]4[Note: Temporaries, unnamed lvalues, and named lvalues that do not have external linkage are not acceptable template-arguments when the corresponding template-parameter has reference type.
[Example:template<const int& CRI> struct B { /* ... */ };B<1> b2;// error: temporary would be required for template argumentint c = 1;B<c> b1;// OK—end example] —end note]5The following conversions are performed on each expression used as a non-type template-argument. If anon-type template-argument cannot be converted to the type of the corresponding template-parameter thenthe program is ill-formed.— for a non-type template-parameter of integral or enumeration type, integral promotions (4.5) and integral conversions (4.7) are applied.242© ISO/IECISO/IEC 14882:1998(E)14 Templates14.3.2 Template non-type arguments— for a non-type template-parameter of type pointer to object, qualification conversions (4.4) and thearray-to-pointer conversion (4.2) are applied. [Note: In particular, neither the null pointer conversion(4.10) nor the derived-to-base conversion (4.10) are applied.
Although 0 is a valid template-argumentfor a non-type template-parameter of integral type, it is not a valid template-argument for a non-typetemplate-parameter of pointer type. ]— For a non-type template-parameter of type reference to object, no conversions apply. The type referredto by the reference may be more cv-qualified than the (otherwise identical) type of the templateargument. The template-parameter is bound directly to the template-argument, which must be anlvalue.— For a non-type template-parameter of type pointer to function, only the function-to-pointer conversion(4.3) is applied. If the template-argument represents a set of overloaded functions (or a pointer to such),the matching function is selected from the set (13.4).— For a non-type template-parameter of type reference to function, no conversions apply.
If thetemplate-argument represents a set of overloaded functions, the matching function is selected from theset (13.4).— For a non-type template-parameter of type pointer to member function, no conversions apply. If thetemplate-argument represents a set of overloaded member functions, the matching member function isselected from the set (13.4).— For a non-type template-parameter of type pointer to data member, qualification conversions (4.4) areapplied.[Example:template<const int* pci> struct X { /* ... */ };int ai[10];X<ai> xi;// array to pointer and qualification conversionsstruct Y { /* ... */ };template<const Y& b> struct Z { /* ...
*/ };Y y;Z<y> z;// no conversion, but note extra cv-qualificationtemplate<int (&pa)[5]> struct W { /* ... */ };int b[5];W<b> w;// no conversionvoid f(char);void f(int);template<void (*pf)(int)> struct A { /* ... */ };A<&f> a;// selects f(int)—end example]14.3.3 Template template arguments[temp.arg.template]1A template-argument for a template template-parameter shall be the name of a class template, expressed asid-expression.
Only primary class templates are considered when matching the template template argumentwith the corresponding parameter; partial specializations are not considered even if their parameter listsmatch that of the template template parameter.2Any partial specializations (14.5.4) associated with the primary class template are considered when a specialization based on the template template-parameter is instantiated.
If a specialization is not visible at thepoint of instantiation, and it would have been selected had it been visible, the program is ill-formed; nodiagnostic is required. [Example:243ISO/IEC 14882:1998(E)© ISO/IEC14.3.3 Template template arguments14 Templatestemplate<class T> class A {// primary templateint x;};template<class T> class A<T*> { // partial specializationlong x;};template<template<class U> class V> class C {V<int> y;V<int*> z;};C<A> c;// V<int> within C<A> uses the primary template,// so c.y.x has type int// V<int*> within C<A> uses the partial specialization,// so c.z.x has type long—end example]14.4 Type equivalence1[temp.type]Two template-ids refer to the same class or function if their template names are identical, they refer to thesame template, their type template-arguments are the same type, their non-type template-arguments of integral or enumeration type have identical values, their non-type template-arguments of pointer or referencetype refer to the same external object or function, and their template template-arguments refer to the sametemplate.