Thread: Declaration inside for statement - scope according to the standard

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.

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, 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