See what's going on with flipcode!

This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.


  Fix MSVC's "for"
  Submitted by

Many people probably know this already, but:

MSVC has a small but annoying bug with the scope of variables defined in for loops. The following fragment is valid c++ but will not compile on msvc.

for(int i=0; i<10; i++) {

for(int i=0; i<10; i++) { DoSomeThingElse(); }

MSVC complains about the second for loop, because the variable 'i' is defined as if it were scoped just outside of the first for loop.

a quick one liner to put at the top of your file, (or even better, in your msvc-specific header files, since everyone is of course targetting multiple platforms and compilers :)):

#define for if(false) else for 

now, for loops will have the semantics that they should, with one small exception: should you type/syntax err badly enough to have an 'else' clause right after your for loop, the compiler will not catch the error... But I've never heard of this happening anyway.

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.


Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.