What does the C++ standard say?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: What does the C++ standard say?

Hybrid View

  1. #1
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    What does the C++ standard say?

    What does the C++ standard say with regards to the following piece of code. I am attempting to compile someone else's code on two different compilers, and on one it fails. The code is something like this:

    Code:
    const int iMax = 50;
    
    void SomeFunction()
    {
        for(int i=0; i<iMax; ++i)
            doSomething(i);
    
        for(int i=0; i<iMax; ++i)
            doSomethingElse(i);
    }
    The reason one fails is it says that i is defined twice (in both for statements). So the question I have is does defining a variable within a for loop limit the scope of the variable to the for loop? Or does it effectively define the variable in the scope before the loop? Which compiler is more compliant to the standard in this particular area?

    Thanks,

    Kevin

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640
    The standard says that "i" is only valid *inside* the scope of the for loop.

    The problem is that some compilers treat:
    Code:
    for (int i = 0; i < 10; ++i)
    as:
    Code:
    int i;
    for (i = 0; i < 10; ++i)
    Funny thing, the compile that does NOT issure the warning is correct, in this case. However, don't expect "i" to be valid outside either loop.

    Viggy

  3. #3
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515

    Re: What does the C++ standard say?

    I know this isn't the VC++ forum, but...
    MSVC++ doesn't adhere to the standard, but the 7.x versions of the MS compiler offer a switch that you can use to force compliance in this respect. From MSDN:
    The C++ standard says that a variable declared in a for loop shall go out of scope after the for loop ends. For example:
    Code:
    for (int i = 0 ; i < 5 ; i++) {
       // do something
    }
    // i is now out of scope under /Za or /Zc:forScope
    By default, under /Ze, a variable declared in a for loop remains in scope until the for loop's enclosing scope ends.

    /Zc:forScope enables standard behavior of variables declared in for loops without needing to specify /Za.

    It is also possible to use the scoping differences of the for loop to redeclare variables under /Ze as follows:
    Code:
    // for_statement5.cpp
    int main()
    {
       int i = 0;   // hidden by var with same name declared in for loop
       for ( int i = 0 ; i < 3; i++ )
       {
       }
    
       for ( int i = 0 ; i < 3; i++ )
       {
       }
    }
    This more closely mimics the standard behavior of a variable declared in a for loop, which requires variables declared in a for loop to go out of scope after the loop is done. When a variable is declared in a for loop, the compiler internally promotes it to a local variable in the for loop's enclosing scope even if there is already a local variable with the same name.
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  4. #4
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Thanks guys! That is the answer I needed.

  5. #5
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Kevin,

    if you need paragraph and verse:
    The Standard, 6.5.3 / 3:
    If the forinitstatement
    is a declaration, the scope of the name(s) declared extends to the end of the forstatement.
    [Example:
    int i = 42;
    int a[10];
    for (int i = 0; i < 10; i++)
    a[i ] = i;
    int j = i; // j = 42
    —end example]
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  6. #6
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    I didn't need paragraph and verse, but thanks though!

    I just wanted to know what the standard required.

    - Kevin

  7. #7
    Join Date
    Apr 2003
    Posts
    893
    Originally posted by KevinHall
    I didn't need paragraph and verse, but thanks though!

    I just wanted to know what the standard required.

    - Kevin
    Why dont you buy the Standard ?? It is about $280 (2003 version)
    You will know alot ofdifferent things..

    Fiona

  8. #8
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    Originally posted by hometown
    Why dont you buy the Standard ?? It is about $280 (2003 version)
    You will know alot ofdifferent things..

    Fiona
    read the question!
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  9. #9
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Originally posted by hometown
    Why dont you buy the Standard ?? It is about $280 (2003 version)
    I can't personally afford it. And right now, the company I work for is financially tight and there are more important things for me to ask to put on the company budget than a copy of the standard.

    - Kevin

  10. #10
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by hometown
    Why dont you buy the Standard ?? It is about $280 (2003 version)
    You will know alot ofdifferent things..

    Fiona
    I think that is for the printed copy. I think the electronic copy is much less.

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471
    $18 for the PDF version from the ANSI store. Well worth it.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  12. #12
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Interesting.... ANSI sells "INCITS/ISO/IEC 14882-1998" (1998 C++ standard) for $18 dollars in PDF format.

    ANSI also sells "ISO/IEC 14882:2003" for $273.

    Is there a new ISO standard for C++? I have not heard anything? If not, then what is this other document for? Are there any changes in this newer document?

    But, you are correct Graham, $18 is worth it. Thanks!

  13. #13
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    I guess the 2003 version is revised and some errata are fixed.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  14. #14
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by KevinHall
    Interesting.... ANSI sells "INCITS/ISO/IEC 14882-1998" (1998 C++ standard) for $18 dollars in PDF format.

    ANSI also sells "ISO/IEC 14882:2003" for $273.

    Is there a new ISO standard for C++? I have not heard anything? If not, then what is this other document for? Are there any changes in this newer document?

    But, you are correct Graham, $18 is worth it. Thanks!
    You should tell us where you are getting your information from, since it seems to be very incomplete and misleading. It is probably not accurate, and if it is not accurate, then it is especially important to specify the source.

    As far as I know, it is very misleading to mention ANSI in this context. The only possible purpsoe I have heard that ANSI can serve for this is that for Americans, they supposedly can sell the standard for less. However as far as I know, that is a rumor that is not true.

    See Techstreet - Product Details: Programming Languages - C++; I think that is current and accurate.

  15. #15
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center