# (c++) warning when local variable is overriden by another local variable

This topic is 3211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Do you know a way to have Visual Studio spit out a warning in the following scenario:
float f = 0.0f;
// ... (many lines of code)
if(...)
{
float f = 10.0f;
// ...
}
// ... (use f)

I've tried with level-4 warnings on VS2008, the compiler is (understandably) perfectly ok with it.

##### Share on other sites
I don't know of any way to make the compiler complain about this, but I think it's perfectly reasonable that it doesn't. One of the advantages of limited scopes is that collisions are not a problem.

##### Share on other sites
This is actually very nice functionality (especially when coming from Java which doesn't even allow this).

##### Share on other sites
I consider it both dangerous and bad practice, which is why I wish visual studio had a warning (people who like to use that language functionality could still disable it)
But that's just me of course. Thanks anyway.

##### Share on other sites
You're probably going to have to use a static code analysis tool, along the lines of lint in order to get warnings for this behaviour.
I'm not experienced with these tools, so unfortunately I can't tell you which (or any) support this warning.

##### Share on other sites
Quote:
 Original post by alvaroI don't know of any way to make the compiler complain about this, but I think it's perfectly reasonable that it doesn't. One of the advantages of limited scopes is that collisions are not a problem.

Though its a nasty habit to pick up if you ever plan on working with a dynamic language.

##### Share on other sites
Quote:
 Original post by janta// ... (many lines of code)
That's a bit of its own problem in a way, isn't it?

##### Share on other sites
Quote:
Original post by nobodynews
Quote:
 Original post by janta// ... (many lines of code)
That's a bit of its own problem in a way, isn't it?

It is indeed but well, real world projects, stuff like that... Stuff isn't always done the way we wish it were.

And not so many lines are necessary to make a programmer waste a handful of hours figuring out why "f" does not have the value he expects it to have. Especially when he's looking at complex code and suspects an entirely different problem... like a thread synchronization issue... x-(

##### Share on other sites
You can try compiling with /Wall, if there's a warning for such a thing you'll see it.

##### Share on other sites
That reminds me that a bunch of co-workers and I think that
if ( conditional );{    DoSomething();}

Should be a warning. There are almost 0 cases where you'd actually want that to be correct, but a lot of cases (careless copy-paste) where you can quickly end up with that code.

The variable masking is annoying too. My favorite problem there is that I'd like to know when I have:
for( int i = 0; i < 10; ++i )    for( int i = 0; i < 10; ++i )        foo.bar;

Again, symptomatic of lazy copy paste(and nondescript loop counter naming), but still rather annoying when it happens.

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632204
• Total Posts
3004755

×