From: daniel.kruegler@googlemail.com   
      
   Am 24.03.2012 06:31, schrieb Frank Birbacher:   
      
   > Working with MSVC and porting code to GCC I was surprised to learn   
   > that the new range based for loop statement requires a variable   
   > declaration:   
   >   
   > vector vec;   
   >   
   > // C++11:   
   > for(int i : vec) {...}   
   >   
   > // MSVC 2010:   
   > for each(int i in vec) {...}   
   >   
   > // does work on MSVC 2010:   
   > int i;   
   > for each(i in vec) {...}   
   >   
   > // not allowed in C++11, but I want it to work:   
   > int i;   
   > for(i : vec) {...}   
   >   
   > The traditional for loop does not require a declaration, but the new   
   > one does. I cannot see a reason why other allowed for the new loop.   
   > Especially when the given "equivalent statement" in the standard   
   > would literally allow the last use case of mine. Was it on purpose?   
   > On what reason?   
      
   The range-based for-loop was introduced as a useful tool that   
   simplifies the most typical loop form, which is: Iterating over some   
   range and initializing a loop-local variable that can be used in the   
   user-provided statement. This is easy to understand and hard to use   
   incorrectly because of locally introduced names. This as a deliberate   
   design decision: It was not specifically designed to support *all*   
   features of the traditional loop, because you still *can* use the   
   traditional loop, if you want.   
      
   I agree that it should be technically possible to extend the language   
   in a way that would allow for an alternative form of the range-based   
   for-loop where instead of a   
      
   for-range-declaration   
      
   you could also provide an expression that could be used as the   
   left-hand side of an assignment expression.   
      
   The question is, whether this extension outweighs disadvantages from   
   that freedom: Was this a simple user-error forgetting to provide a   
   type or was assignment intended:   
      
   int i;   
      
   ... // Some code   
      
   vector vec;   
   for each(i : vec) {...}   
      
   Or was is really intended to be an assignment of an existing variable?   
   There is a good chance that providing this extension could prevent the   
   design idea of being rather fool-proof.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|