From: siliconmunky on
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?

Thanks.

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

From: Hakusa on
Here's a larger example to look at:

-------------------source---------------
#include <iostream>
#include <stdlib.h>
using namespace std;

int x = 1;

void printX ()
{
cout << "Global x = " << x;
}

int main()
{
cout << "Global x = " << x << endl;

int x = 2;

cout << "In main x = " << x << endl;

if( true )
{
int x = 3;
cout << "In condition x = " << x << endl;
}

cout << "In main x = " << x << endl;

atexit( printX );
}
-------------------/source---------------

-------------------output----------------
Global x = 1
In main x = 2
In condition x = 3
In main x = 2
Global x = 1
------------------/output----------------

Nested variable declarations was a term I'd never heard before, so I
googled it. First for C++. I got absolutely no worth-my-time findings,
so I'm thinking that C++ doesn't support them. Or maybe, I still don't
understand the term, but either way...

C++, in my example, created a variable in global scope, that I
printed. When I declared another variable of the same name, C++ simply
decided that it's scope was local and local variables trump globals.
So when I printed the second time, it printed the local variable.

You might already know this, but I need to make sure we're speaking on
the same level, seeing as we're speaking with different terms.

This feature can really help you not make an idiot of yourself, some
might say, when you declare things locally. If I include a file that
uses a global variable, and I make a variable of the same name and use
it locally, I can do so because mine has a local scope and the
compiler won't get confused when I call the included file's functions.
There are millions of examples along those lines.

But, a lot of coders will say that it's a bad programming practice to
have two variables of the same name. C++ would crash with a multiple
definitions rule when you did this, and force you to rename it.

I don't think it's good or bad, but this is what a language is: A
philosophy on what practices should be forced, which should be
encouraged, and which are to be optional.

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

From: Tony Delroy on
On Apr 23, 1:19 pm, siliconmu...(a)gmail.com wrote:
> Hi, I was wondering what is the point of the language supporting
> nested variable declarations of the same name?
> [snip]
> 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?

Nested variables help decouple the implementation of a function from
the calling context.

Imagine you write 'inline double factorial(int)', put it in a header
file, and tell someone "I've got this great function, try it out".
With your idea above, they would have to change their code to make
sure they didn't have a variable 'i' anywhere in their call stack
before they could call your factorial function....

Put the other way around, if you want to write your factorial(int n)
function, you should be able to create an "int i" inside without
worrying whether there's an int i elsewhere: your function can't
access or change anything of the caller's that the caller doesn't
provide explicit access to, so what's the point in knowing about it?

HTH, Tony

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

From: Francis Glassborow 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:
>
> 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?
>
That is because such nesting may in fact be a consequence of including a
third party library. Just because that library elects to add a new
global variable in its next release should not cause me to have to
rewrite my code.

Please also note that in fact the two definitions above do NOT define
exactly the same name because the compiler always includes scope so the
first one is actually ::a

Now I would have some sympathy if the case were:

void foo{}{
int i(0);
{
int i(1);
//do something
}
// do something else
}

However trying to write rules for such special cases often results in
fragile code. So we just resort to trusting the programmer while
protecting her from problems created by other programmers.

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

From: Bo Persson 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?

The general idea of nested scopes is to allow this. :-)


>
> 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?

Some compilers can issue a warning for hiding names, if you set the
warning level high enough (or enable extra warnings).

On the other hand, if adding

int i, j;

at the outermost scope of a program would reserve these names
everywhere else, you could easily break most for loops ever written.


Bo Persson


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