Zählvariable außerhalb von for benutzbar?
-
im c/c++ quellcodes die mit ms visual c++ geschrieben wurden, finde ich häufig folgende schleifenkonstrukte.
for (int i = 0; i < Irgendwas; i++) { // ... was machen ... if (Bedingung_erfuellt) break; } // Zählvariable außerhalb weiterbenutzen ! if (i == xyz) { // ... blah }ist das mal wieder winzigweich-mist?
-
Dann bist du vermutlich mit dem VC++ 6 zugange. Und dann ist das kein "Winzigweich"-Mist, sondern zum Zeitpunkt der Programmierung von VC++ 6 gab's noch keinen abgenommenen C++ Standard und der Scope von Variablen nach for-Schleifen war unklar. In der Tat ist solcher Code heutzutage falsch, damals war's aber "okay".
-
Es sind in der Tat Erweiterungen von MS.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vcrefZcforScope.asp
Standard behavior is to let a for loop's initializer go out of scope after the for loop. Under /Ze, the for loop's initializer remains in scope until the local scope ends.
The Visual C++ compiler offers a number of features beyond those specified in either the ANSI C or ANSI C++ standards. These features are known collectively as Microsoft's extensions to C and C++. These extensions are available when the /Ze option, the default, is specified and are not available when the /Za option is specified.
-
Jaja, frei nach dem Prinzip "it's not a bug, it's a feature"

-
Der GCC ist aber auch erst in späteren Versionen konstant der Meinung, die Zählvariable ginge bereits am Ende der for-Schleife out-of-scope...