Title
Scope of template parameters
Status
cd2
Section
6.4 [basic.scope]
Submitter
Gabriel Dos Reis

Created on 2004-11-01.00:00:00 last changed 143 months ago

Messages

Date: 2009-10-15.00:00:00

[Voted into WP at October, 2009 meeting.]

Date: 2009-09-15.00:00:00

Proposed resolution (September, 2009):

  1. Insert the following as a new paragraph following 6.4.2 [basic.scope.pdecl] paragraph 8:

  2. The point of declaration of a template parameter is immediately after its complete template-parameter. [Example:
      typedef unsigned char T;
      template<class T
           = T         // Lookup finds the typedef name of unsigned char.
    
           , T         //Lookup finds the template parameter.
               N = 0> struct A {};
    

    end example]

  3. Delete 13.2 [temp.param] paragraph 14:

  4. A template-parameter shall not be used in its own default argument.
    [Drafting note: This change conflicts with the resolution for issue 187 but is in accord with widespread implementation practice.]
  5. Insert the following as a new section following 6.4.7 [basic.scope.enum]:

  6. Template Parameter Scope [basic.scope.temp]

    The declarative region of the name of a template parameter of a template template-parameter is the smallest template-parameter-list in which the name was introduced.

    The declarative region of the name of a template parameter of a template is the smallest template-declaration in which the name was introduced. Only template parameter names belong to this declarative region; any other kind of name introduced by the declaration of a template-declaration is instead introduced into the same declarative region where it would be introduced as a result of a non-template declaration of the same name. [Example:

      namespace N {
        template<class T> struct A{};               // line 2
        template<class U> void f(U){}               // line 3
        struct B {
          template<class V>friend int g(struct C*); // line 5
        };
      }
    

    The declarative regions of T, U and V are the template-declarations on lines 2, 3 and 5, respectively. But the names A, f, g and C all belong to the same declarative region—namely, the namespace-body of N. (g is still considered to belong to this declarative region in spite of its being hidden during qualified and unqualified name lookup.) —end example]

    The potential scope of a template parameter name begins at its point of declaration (6.4.2 [basic.scope.pdecl]) and ends at the end of its declarative region. [Note: this implies that a template-parameter can be used in the declaration of subsequent template-parameters and their default arguments but cannot be used in preceding template-parameters or their default arguments. For example,

      template<class T, T* p, class U = T> class X { /* ... */ }; 
      template<class T> void f(T* p = new T); 
    

    This also implies that a template-parameter can be used in the specification of base classes. For example,

      template<class T> class X : public Array<T> { /* ... */ }; 
      template<class T> class Y : public T { /* ... */ }; 
    

    The use of a template parameter as a base class implies that a class used as a template argument must be defined and not just declared when the class template is instantiated. —end note]

    The declarative region of the name of a template parameter is nested within the immediately-enclosing declarative region. [Note: as a result, a template-parameter hides any entity with the same name in an enclosing scope (_N4868_.6.4.10 [basic.scope.hiding]). [Example:

      typedef int N;
      template<N X, typename N, template<N Y> class T>
        struct A;
    

    Here, X is a non-type template parameter of type int and Y is a non-type template parameter of the same type as the second template parameter of A. —end example] —end note]

    [Note: because the name of a template parameter cannot be redeclared within its potential scope (13.8.2 [temp.local]), a template parameter's scope is often its potential scope. However, it is still possible for a template parameter name to be hidden; see 13.8.2 [temp.local]. —end note]

  7. Delete 13.2 [temp.param] paragraph 13, including the example:

  8. The scope of a template-parameter extends...
  9. Delete 13.8.2 [temp.local] paragraph 6, including the note and example:

  10. The scope of a template-parameter extends...
Date: 2013-01-14.00:00:00
N2800 comment FR 16

Sections 6.4.3 [basic.scope.block] to 6.4.6 [basic.scope.class] define and summarize different kinds of scopes in a C++ program. However it is missing a description for the scope of template parameters. I believe a section is needed there — even though some information may be found in clause 14.

History
Date User Action Args
2010-03-29 00:00:00adminsetstatus: dr -> cd2
2009-11-08 00:00:00adminsetmessages: + msg2422
2009-11-08 00:00:00adminsetstatus: tentatively ready -> dr
2009-09-29 00:00:00adminsetmessages: + msg2289
2009-09-29 00:00:00adminsetstatus: drafting -> tentatively ready
2009-03-23 00:00:00adminsetstatus: open -> drafting
2004-11-01 00:00:00admincreate