What is the potential scope of a template paramete

2019-04-16 02:52发布

A point from ISO draft n3290 section 3.3.9 paragraph 5:

Because the name of a template parameter cannot be redeclared within its potential scope (14.6.1), a template parameter’s scope is often its potential scope. However, it is still possible for a template parameter name to be hidden;

What does "potential scope" mean in this context? Can anybody provide an example of such?

draft link n3290: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3290.pdf

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-16 03:49

From paragraph 3 of the same section:

The potential scope of a template parameter name begins at its point of declaration (3.3.2) 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 ]

查看更多
萌系小妹纸
3楼-- · 2019-04-16 03:51

3.3.1 : The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case, the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.

Normally, this refers to cases like this:

void Foo(int i) {
  {
    int i = 5;
    std::cout << i;
  }
  std::cout << i;
};

The potential scope of the second i is excluded from the scope of the first i. In other words, this describes precisely where name hiding applies. The bit you quote says that template names can be hidden, too.

查看更多
登录 后发表回答