Declaration inside for statement - scope according to the standard

This is a discussion on Declaration inside for statement - scope according to the standard within the C++ Programming forums, part of the General Programming Boards category; That's interesting. It also means that this is legal: Code: while (int i = 1) { int i = 0; ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    That's interesting. It also means that this is legal:
    Code:
    while (int i = 1) {
        int i = 0;
    }
    Though personally I always thought it was not, and hence read the standard for the for loop with this assumption.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    It also means that this is legal:
    this is not clear... The condition in the () is checked on every iteration...
    does it mean that the int i is executed on every iteration? then the scope of the variable will be the same as the second i...
    if not - it will be something very tricky...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    More or less. This will help.

    Again according to the standard:
    Quote Originally Posted by 6.5.1.2

    while (T t = x) statement

    is equivalent to

    Code:
    label:
        { // start of condition scope
            T t = x; <-- notice the object creation location. 
            if (t) { <-- new scope here
                statement
                goto label; <-- condition object destruction
           }
        } // end of condition scope
    The object created in a condition is destroyed and created with each iteration of the loop.
    Last edited by Mario F.; 11-29-2006 at 08:59 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 01:42 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 08:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21