From: Chris Uzdavinis on
On Apr 23, 12:19 am, siliconmu...(a)gmail.com wrote:
> Hi, I was wondering what is the point of the language supporting
> nested variable declarations of the same name?
> ...
> int a = 0;
> if( some_bool ) {
> int a = 5;
> ...
> }
>
> cout << a; //this outputs 0
>
> I can't think of a reason why this would be supported, and I feel the
> compiler should throw a warning. So, is there a good useful reason to
> allow this?

What would the point of a "scope" be if you could not do this? Why
shouldn't this be allowed?

That said, some compilers can emit diagnostics when they detect "hiding"
or "shadowing" another name. I have to admit to having found it more
annoying than useful, but that is probably personal taste.

Chris

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Erik Wikström on
On 2008-04-23 06:19, siliconmunky(a)gmail.com wrote:
> Hi, I was wondering what is the point of the language supporting
> nested variable declarations of the same name?
>
> For example:
>
> int a = 0;
>
> if( some_bool )
> {
> int a = 5;
> ...
> }
>
> cout << a; //this outputs 0
>
>
> I can't think of a reason why this would be supported, and I feel the
> compiler should throw a warning. So, is there a good useful reason to
> allow this?

I can not come up with a good reason why it should be disallowed, and I
suspect that was the motivation to allow it.

--
Erik Wikström


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
siliconmunky(a)gmail.com wrote:
> int a = 0;
>
> if( some_bool )
> {
> int a = 5;
> ...
> }
>
> cout << a; //this outputs 0
>
>
> I can't think of a reason why this would be supported, and I feel the
> compiler should throw a warning. So, is there a good useful reason to
> allow this?

Yes, in particular reusing the same name for loop variables. Of course, that
is only for reuse in the same function, for globals it makes even more
sense because your code shouldn't change its meaning when someone adds a
global with a similar name.

Uli

--
Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
siliconmunky(a)gmail.com wrote:
> Hi, I was wondering what is the point of the language supporting
> nested variable declarations of the same name?
>
> For example:
[snipped]
>
> I can't think of a reason why this would be supported, and I feel the
> compiler should throw a warning. So, is there a good useful reason to
> allow this?

Otherwise:

* When a name is introduced in the global scope (by using a library or
something), every class or function declaration that uses that name
should be modified.

* When a class or function declaration is already using a name, you
cannot declare something with the same name in the global scope.

Would you really like those to happen?
The idea of scoping is precisely (or mostly) to avoid them from happening.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]