Scope rules

Discussion in 'C++' started by allspamgoeshere3@hotmail.com, Sep 14, 2005.

  1. Guest

    Hi!
    Is the following code really correct according to the C++-standard?

    int main()
    {
    for(unsigned int i = 0; i < 10; ++i)
    ;

    i = 0;

    return 0;
    }

    I thougt i would be interpreted as local variable part of the for loop
    but Visual Studio .NET 2003 seams to accept the code without
    complaining.

    /H
     
    , Sep 14, 2005
    #1
    1. Advertising

  2. wrote:

    > Is the following code really correct according to the C++-standard?


    > cat main.cpp

    int main(int argc, char* argv[]) {
    for(unsigned int i = 0; i < 10; ++i) ;

    i = 0;

    return 0;
    }

    > g++ -Wall -ansi -pedantic -o main main.cpp

    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:4: error: \
    name lookup of ‘i’ changed for new ISO ‘for’ scoping
    main.cpp:2: error: using obsolete binding at ‘i’
     
    E. Robert Tisdale, Sep 14, 2005
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > Hi!
    > Is the following code really correct according to the C++-standard?


    No, it isn't.

    >
    > int main()
    > {
    > for(unsigned int i = 0; i < 10; ++i)
    > ;
    >
    > i = 0;
    >
    > return 0;
    > }
    >
    > I thougt i would be interpreted as local variable part of the for loop
    > but Visual Studio .NET 2003 seams to accept the code without
    > complaining.


    AFAIK, this is a legacy. There is some compiler switch to make the compiler
    correctly produce an error message for this code.
     
    Rolf Magnus, Sep 14, 2005
    #3
  4. red floyd Guest

    Rolf Magnus wrote:
    > wrote:


    >
    > AFAIK, this is a legacy. There is some compiler switch to make the compiler
    > correctly produce an error message for this code.
    >


    it's /Zc:forScope
     
    red floyd, Sep 15, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?=22J=2EJ=2EGarc=EDa=22?=

    Special scope rules for static operators?

    =?ISO-8859-1?Q?=22J=2EJ=2EGarc=EDa=22?=, May 29, 2005, in forum: C++
    Replies:
    4
    Views:
    446
    =?UTF-8?B?IkouSi5HYXJjw61hIg==?=
    May 29, 2005
  2. Kevin Spencer

    Re: QUERY: 'On Error Resume Next' scope rules

    Kevin Spencer, Jun 17, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    596
    Kevin Spencer
    Jun 17, 2006
  3. Andrew Collier

    scope rules in nested functions

    Andrew Collier, Jan 29, 2005, in forum: Python
    Replies:
    0
    Views:
    311
    Andrew Collier
    Jan 29, 2005
  4. fctk

    Scope Rules

    fctk, Apr 16, 2006, in forum: C Programming
    Replies:
    3
    Views:
    494
  5. n2xssvv g02gfr12930

    Scope rules

    n2xssvv g02gfr12930, Jul 20, 2006, in forum: C++
    Replies:
    3
    Views:
    367
    n2xssvv g02gfr12930
    Jul 20, 2006
Loading...

Share This Page