Стандарт C++ 11 (1119564), страница 97
Текст из файла (страница 97)
— end example ]10If a name does not depend on a template-parameter (as defined in 14.6.2), a declaration (or set of declarations)for that name shall be in scope at the point where the name appears in the template definition; the name isbound to the declaration (or declarations) found at that point and this binding is not affected by declarationsthat are visible at the point of instantiation. [ Example:void f(char);template<class T> void g(T t) {f(1);// f(char)f(T(1));// dependentf(t);// dependentdd++;// not dependent// error: declaration for dd not found}enum E { e };void f(E);double dd;void h() {g(e);g(’a’);// will cause one call of f(char) followed// by two calls of f(E)// will cause three calls of f(char)}— end example ]11[ Note: For purposes of name lookup, default arguments of function templates and default arguments ofmember functions of class templates are considered definitions (14.5).
— end note ]14.6.11Locally declared names[temp.local]Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injectedclass-name can be used as a template-name or a type-name. When it is used with a template-argument-list,§ 14.6.1© ISO/IEC 2011 – All rights reserved355ISO/IEC 14882:2011(E)as a template-argument for a template template-parameter, or as the final identifier in the elaborated-typespecifier of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalentto the template-name followed by the template-parameters of the class template enclosed in <>.2Within the scope of a class template specialization or partial specialization, when the injected-class-name isused as a type-name, it is equivalent to the template-name followed by the template-arguments of the classtemplate specialization or partial specialization enclosed in <>.
[ Example:template<template<class> class T> class A { };template<class T> class Y;template<> class Y<int> {Y* p;// meaningY<char>* q;// meaningA<Y>* a;// meaningclass B {template<class> friend class Y;// meaning};};Y<int>Y<char>A<::Y>::Y— end example ]3The injected-class-name of a class template or class template specialization can be used either as a templatename or a type-name wherever it is in scope.
[ Example:template <class T> struct Base {Base* p;};template <class T> struct Derived: public Base<T> {typename Derived::Base* p;// meaning Derived::Base<T>};template<class T, template<class> class U = T::template Base> struct Third { };Third<Base<int> > t;// OK: default argument uses injected-class-name as a template— end example ]4A lookup that finds an injected-class-name (10.2) can result in an ambiguity in certain cases (for example, if itis found in more than one base class). If all of the injected-class-names that are found refer to specializationsof the same class template, and if the name is used as a template-name, the reference refers to the classtemplate itself and not a specialization thereof, and is not ambiguous.
[ Example:template <class T> struct Base { };template <class T> struct Derived: Base<int>, Base<char> {typename Derived::Base b;// error: ambiguoustypename Derived::Base<double> d;// OK};— end example ]5When the normal name of the template (i.e., the name from the enclosing scope, not the injected-class-name)is used, it always refers to the class template itself and not a specialization of the template. [ Example:template<class T> class X {X* p;// meaning X<T>X<T>* p2;X<int>* p3;§ 14.6.1356© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)::X* p4;// error: missing template argument list// ::X does not refer to the injected-class-name};— end example ]6A template-parameter shall not be redeclared within its scope (including nested scopes).
A templateparameter shall not have the same name as the template name. [ Example:template<class T, int i> class Y {int T;// error: template-parameter redeclaredvoid f() {char T;// error: template-parameter redeclared}};template<class X> class X;// error: template-parameter redeclared— end example ]7In the definition of a member of a class template that appears outside of the class template definition, thename of a member of the class template hides the name of a template-parameter of any enclosing classtemplates (but not a template-parameter of the member if the member is a class or function template).[ Example:template<class T> struct A {struct B { /* ...
*/ };typedef void C;void f();template<class U> void g(U);};template<class B> void A<B>::f() {B b;// A’s B, not the template parameter}template<class B> template<class C> void A<B>::g(C) {B b;// A’s B, not the template parameterC c;// the template parameter C, not A’s C}— end example ]8In the definition of a member of a class template that appears outside of the namespace containing theclass template definition, the name of a template-parameter hides the name of a member of this namespace.[ Example:namespace N {class C { };template<class T> class B {void f(T);};}template<class C> void N::B<C>::f(C) {C b;// C is the template parameter, not N::C}§ 14.6.1© ISO/IEC 2011 – All rights reserved357ISO/IEC 14882:2011(E)— end example ]9In the definition of a class template or in the definition of a member of such a template that appears outsideof the template definition, for each base class which does not depend on a template-parameter (14.6.2), ifthe name of the base class or the name of a member of the base class is the same as the name of a templateparameter, the base class name or member name hides the template-parameter name (3.3.10).
[ Example:struct A {struct B { /∗ ... ∗/ };int a;int Y;};template<class B, class a> struct X : A {B b;// A’s Ba b;// error: A’s a isn’t a type name};— end example ]14.6.21Dependent names[temp.dep]Inside a template, some constructs have semantics which may differ from one instantiation to another. Such aconstruct depends on the template parameters. In particular, types and expressions may depend on the typeand/or value of template parameters (as determined by the template arguments) and this determines thecontext for name lookup for certain names. Expressions may be type-dependent (on the type of a templateparameter) or value-dependent (on the value of a non-type template parameter). In an expression of theform:postfix-expression ( expression-listopt )where the postfix-expression is an id-expression, the id-expression denotes a dependent name if— any of the expressions in the expression-list is a pack expansion (14.5.3),— any of the expressions in the expression-list is a type-dependent expression (14.6.2.2), or— if the unqualified-id of the id-expression is a template-id in which any of the template argumentsdepends on a template parameter.If an operand of an operator is a type-dependent expression, the operator also denotes a dependent name.Such names are unbound and are looked up at the point of the template instantiation (14.6.4.1) in both thecontext of the template definition and the context of the point of instantiation.2[ Example:template<class T> struct X : B<T> {typename T::A* pa;void f(B<T>* pb) {static int i = B<T>::i;pb->j++;}};the base class name B<T>, the type name T::A, the names B<T>::i and pb->j explicitly depend on thetemplate-parameter.
— end example ]§ 14.6.2358© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)3In the definition of a class or class template, if a base class depends on a template-parameter, the base classscope is not examined during unqualified name lookup either at the point of definition of the class templateor member or during an instantiation of the class template or member. [ Example:typedef double A;template<class T> class B {typedef int A;};template<class T> struct X : B<T> {A a;// a has type double};The type name A in the definition of X<T> binds to the typedef name defined in the global namespace scope,not to the typedef name defined in the base class B<T>.
— end example ] [ Example:struct A {struct B { /∗ ... ∗/ };int a;int Y;};int a;template<class T> struct Y : T {struct B { /∗ ... ∗/ };B b;// The B defined in Yvoid f(int i) { a = i; }// ::aY* p;// Y<T>};Y<A> ya;The members A::B, A::a, and A::Y of the template argument A do not affect the binding of names in Y<A>.— end example ]14.6.2.11Dependent types[temp.dep.type]A name refers to the current instantiation if it is— in the definition of a class template, a nested class of a class template, a member of a class template, ora member of a nested class of a class template, the injected-class-name (Clause 9) of the class templateor nested class,— in the definition of a primary class template or a member of a primary class template, the name of theclass template followed by the template argument list of the primary template (as described below)enclosed in <> (or an equivalent template alias specialization),— in the definition of a nested class of a class template, the name of the nested class referenced as amember of the current instantiation, or— in the definition of a partial specialization or a member of a partial specialization, the name of theclass template followed by the template argument list of the partial specialization enclosed in <> (oran equivalent template alias specialization).
If the nth template parameter is a parameter pack, thenth template argument is a pack expansion (14.5.3) whose pattern is the name of the parameter pack.§ 14.6.2.1© ISO/IEC 2011 – All rights reserved359ISO/IEC 14882:2011(E)2The template argument list of a primary template is a template argument list in which the nth templateargument has the value of the nth template parameter of the class template. If the nth template parameteris a template parameter pack (14.5.3), the nth template argument is a pack expansion (14.5.3) whose patternis the name of the template parameter pack.3A template argument that is equivalent to a template parameter (i.e., has the same constant value or thesame type as the template parameter) can be used in place of that template parameter in a reference tothe current instantiation.