2
votes

A point from ISO draft n3290 section 3.3.9 paragraph 4:

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 (3.3.10).

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

Can any one please ....tell some other example other than this ..where & where this situation erises

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

2
Just curious to know, what is the purpose of , typename N, in the template declaration ?iammilind
@iammilind: You should probably read some basic tutorial on templates. In that context typename and class are equivalent, if that answers your question. If it does not, then it declares a type template argument named N, which in turns creates a local type alias N in the scope of the template to the type used as second argument to the template during instantiation. In that particular example, the intention is that as N is also a type at namespace level, this parameter hides the typedef at namespace scope.David Rodríguez - dribeas

2 Answers

3
votes

I think it is quite obvious: any time that an entity in the scope of the template declaration has the same name as one of the template parameters, the name will be hidden within the template. If you are having problems coming up with examples chances are that you did not understand the meaning of the quote. What is it that you understood? What is it that you have troubles understanding?

Also for those that don't want to go into the standard, it is important to complete the original quote:

   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 ]

Which helps understanding the example. Note what the quote means: the first N refers to the typedef at the namespace scope, then a template argument N is introduced. From there on, the template argument hides the outer N, and the next appearance of N refers to the template argument and not to the outer N (only within the declarative scope of the template)

3
votes

Sure:

int n = 0;

void f() {
   int n = 42;
}

The n in the function hides the one declared at global scope.