Should this compile?

Discussion in 'C++' started by Xavier Decoret, Aug 26, 2003.

  1. for (int i=0;i<10;++i)
    {
    int i = -1;
    cout<<i<<endl;
    }

    As far as I understand, the int declaration in the for statement makes
    it declared for the scope of the for loop. So it would not be possible
    to declare another int i.

    On gcc-3.2.3, the above code does compile and displays 10 lines of -1.

    Is it a standard compliant behaviour?

    --
    +-------------------------------------------------+
    | Xavier D├ęcoret - Post Doct |
    | Graphics Lab (LCS) - MIT |
    | mailto: |
    | home : http://www.graphics.lcs.mit.edu/~decoret|
    +-------------------------------------------------+
    Xavier Decoret, Aug 26, 2003
    #1
    1. Advertising

  2. "Xavier Decoret" <> wrote in message
    news:bifsjb$19e6$...
    > for (int i=0;i<10;++i)
    > {
    > int i = -1;
    > cout<<i<<endl;
    > }
    >
    > As far as I understand, the int declaration in the for statement makes
    > it declared for the scope of the for loop. So it would not be possible
    > to declare another int i.


    No that's wrong. A second declaration is possible, it hides the outer
    declaration.

    >
    > On gcc-3.2.3, the above code does compile and displays 10 lines of -1.
    >
    > Is it a standard compliant behaviour?


    Yes.

    john
    John Harrison, Aug 26, 2003
    #2
    1. Advertising

  3. Xavier Decoret

    Fogus Guest

    If I remember correctly, the C++ grammar allows multiple declarations
    of the same variable names within different scopes. However, I recall
    that the Microsoft compilers did not allow this. I may be confused
    however due to my long hiatis from C++, so hopefully someone will be
    able to provide a definitive answer.

    -m



    Xavier Decoret <> wrote in message news:<bifsjb$19e6$>...
    > for (int i=0;i<10;++i)
    > {
    > int i = -1;
    > cout<<i<<endl;
    > }
    >
    > As far as I understand, the int declaration in the for statement makes
    > it declared for the scope of the for loop. So it would not be possible
    > to declare another int i.
    >
    > On gcc-3.2.3, the above code does compile and displays 10 lines of -1.
    >
    > Is it a standard compliant behaviour?
    Fogus, Aug 26, 2003
    #3
  4. Xavier Decoret

    Ron Natalie Guest

    "Fogus" <> wrote in message news:...
    > If I remember correctly, the C++ grammar allows multiple declarations
    > of the same variable names within different scopes. However, I recall
    > that the Microsoft compilers did not allow this. I may be confused
    > however due to my long hiatis from C++, so hopefully someone will be
    > able to provide a definitive answer.
    >

    You're confused. Even microsoft supports the program as posted. There
    are two scopes, the for loop itself and the compound statement (the stuff
    inside the braces). What Microsoft didn't get right (at least in the usual
    configuration) was they didn't define a new scope for the for loop itself.
    They put the variable declared there in the enclosing scope.
    Ron Natalie, Aug 26, 2003
    #4
  5. "jeffc" <> wrote in message
    news:...
    >
    > "Ron Natalie" <> wrote in message
    > news:3f4b9a5b$0$23258$...
    > >
    > > "Fogus" <> wrote in message

    > news:...
    > > > If I remember correctly, the C++ grammar allows multiple declarations
    > > > of the same variable names within different scopes. However, I recall
    > > > that the Microsoft compilers did not allow this. I may be confused
    > > > however due to my long hiatis from C++, so hopefully someone will be
    > > > able to provide a definitive answer.
    > > >

    > > You're confused. Even microsoft supports the program as posted.

    There
    > > are two scopes, the for loop itself and the compound statement (the

    stuff
    > > inside the braces). What Microsoft didn't get right (at least in the

    > usual
    > > configuration) was they didn't define a new scope for the for loop

    itself.
    > > They put the variable declared there in the enclosing scope.

    >
    > But doesn't this make John Harrison's comment valid only for the (invalid)
    > Microsoft case?
    >


    Comeau C++ rejects the posted code. Looks like I was wrong (along with gcc
    3.2.3).

    john
    John Harrison, Aug 26, 2003
    #5
  6. Xavier Decoret

    Ron Natalie Guest


    >
    > Comeau C++ rejects the posted code. Looks like I was wrong (along with gcc
    > 3.2.3).


    Golly, that's right and danged inconsistant with the rest of the language. A variable
    may not be decleared in the outermost block of the attached statement. An extra set
    of braces would fix that.

    The folowing is legal.

    for (int i=0;i<10;++i)
    { {
    int i = -1;
    cout<<i<<endl;
    } |
    Ron Natalie, Aug 26, 2003
    #6
  7. Xavier Decoret

    Andrew Ward Guest

    Visual C++ rejects the code with:
    error C2374: 'i' : redefinition; multiple initialization



    "Ron Natalie" <> wrote in message
    news:3f4b9a5b$0$23258$...
    >
    > "Fogus" <> wrote in message

    news:...
    > > If I remember correctly, the C++ grammar allows multiple declarations
    > > of the same variable names within different scopes. However, I recall
    > > that the Microsoft compilers did not allow this. I may be confused
    > > however due to my long hiatis from C++, so hopefully someone will be
    > > able to provide a definitive answer.
    > >

    > You're confused. Even microsoft supports the program as posted. There
    > are two scopes, the for loop itself and the compound statement (the stuff
    > inside the braces). What Microsoft didn't get right (at least in the

    usual
    > configuration) was they didn't define a new scope for the for loop itself.
    > They put the variable declared there in the enclosing scope.
    >
    >
    Andrew Ward, Aug 27, 2003
    #7
  8. Xavier Decoret <> wrote in message news:<bifsjb$19e6$>...
    > for (int i=0;i<10;++i)
    > {
    > int i = -1;
    > cout<<i<<endl;
    > }
    >
    > As far as I understand, the int declaration in the for statement makes
    > it declared for the scope of the for loop. So it would not be possible
    > to declare another int i.
    >
    > On gcc-3.2.3, the above code does compile and displays 10 lines of -1.
    >
    > Is it a standard compliant behaviour?


    No. This is how C++ originally worked. At some point the scoping of
    'for' declarations was redefined. Since this broke lots of C++ code,
    many compilers implement the old behavior by default.

    Consider:

    #include <stdio.h>

    int i;

    int main(void)
    {
    for(int i=0; i<10; i++)
    {
    // int i; // legal in old C++; error under ISO C++
    }

    if(i==10)
    printf("Old C++\n");
    else
    printf("ISO C++\n");

    return 0;
    }

    When I invoke my compiler with no options, I get this:

    test.cpp(11) : warning C4288: nonstandard extension used : 'i' : loop
    control variable declared in the for-loop is used outside the for-loop
    scope; it conflicts with the declaration in the outer scope
    test.cpp(7) : definition of 'i' used
    test.cpp(3) : definition of 'i' ignored

    And the program prints "Old C++".

    When invoked in "standard C++" mode, I get this:

    test.cpp(11) : warning C4258: 'i' : definition from the for loop is
    ignored; the definition from the enclosing scope is used
    test.cpp(7) : definition of 'i' ignored
    test.cpp(3) : definition of 'i' used

    And the program prints "ISO C++".

    Sam
    Samuel Barber, Aug 27, 2003
    #8
  9. Xavier Decoret

    Jason Guest

    With GCC you can specify -ansi, but that is for C only, as far as I know.
    However, newer versions* may have a "strict" switch, have a look at the
    command line options.

    *My compiler was a bit old v3.0.2 something, compiled for ARM7 TDMI, that's
    why I am using an older one. However, the -ansi switch for C works pretty
    well.
    Jason, Aug 27, 2003
    #9
    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. William Payne

    Should this compile without cast?

    William Payne, Dec 21, 2003, in forum: C++
    Replies:
    6
    Views:
    355
    Ron Natalie
    Dec 22, 2003
  2. Nagaraj
    Replies:
    1
    Views:
    853
    Lionel B
    Mar 1, 2007
  3. ~~~ .NET Ed ~~~

    How should control images should be handled?

    ~~~ .NET Ed ~~~, Oct 31, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    231
    John Saunders
    Nov 3, 2004
  4. Josef 'Jupp' SCHUGT

    What the FAQs should and should not contain

    Josef 'Jupp' SCHUGT, Aug 19, 2005, in forum: Ruby
    Replies:
    0
    Views:
    193
    Josef 'Jupp' SCHUGT
    Aug 19, 2005
  5. botp
    Replies:
    6
    Views:
    208
    Joel VanderWerf
    Oct 5, 2010
Loading...

Share This Page