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
    24,878
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,758
    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...
    To be or not to be == true

  3. #18
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Angola
    Posts
    8,084
    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 09:59 AM.
    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, 02:42 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 09:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 10:15 AM

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