Argh… C++ (and Visual Studio)

I’m not much of a language zealot, but having recently been poking around Visual Studio 2005, I’m glad to report Microsoft, has finally decided to adopt the 8ish year old “standard” in C++ for the scoping of variables declared in a for loop.
What’s that really mean? In the process of desperately attempting to debug somebody else’s Visual Studio C++ code (a lot of curiously written code), I encountered a construct that made me cringe… Take the following piece of abstracted code (Visual Studio .NET 2003):

#include "stdafx.h"
#using namespace System;

int _tmain()
{
for( int i = 0;i<10;i++ ) { Console::WriteLine( i ); }
Console::WriteLine( i );
return 0;
}

I know I’m bound to embarrass myself, but I originally thought that the variable i was only in scope for the loop. But, .NET compiled it just fine, and it ran. So, time to whip out a much more trustworthy source: gcc. And time to write another test program.

#include iostream
#include string
using namespace std;
int main( int argc, char** argv )
{
for( int i=0;i<10;i++ ) { cout << i << endl; }
cout << i << endl;
}

And, upon compilation — an error “error: name lookup of i changed for new ISO for scoping.” Ah-ha!

For all those that are curious, if you take the above Windows C++ example, and try to compile it in VS 2005, it will fail. Again, nice to know that Microsoft finally caught up to something written on July 20, 1998. So, if you find yourself needing/wanting to compile code written in pre Visual Studio 2005 time into VS2005… good luck! We’ll be needing it too.

 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus
© 2009 Synthesis Studios. All rights reserved. Terms & Conditions | Privacy Policy | Accessibility