What is assigning object inside the class using scope some varoutside the class is illegal?

Discussion in 'C++' started by puzzlecracker, Apr 15, 2008.

  1. Say we have the following class (adapted from the standard):

    enum {i=1};
    class X{
    char v;
    enum{i=2} // why this is illegal?
    int f(){ return sizeof(c); } // and this is legal?
    };
     
    puzzlecracker, Apr 15, 2008
    #1
    1. Advertising

  2. puzzlecracker wrote:
    > Say we have the following class (adapted from the standard):
    >
    > enum {i=1};
    > class X{
    > char v;
    > enum{i=2} // why this is illegal?


    Huh? Aside from the missing semicolon, this is perfectly legal.

    > int f(){ return sizeof(c); } // and this is legal?


    What is legal? 'c' is not declared. How can it possibly be legal?

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Apr 15, 2008
    #2
    1. Advertising

  3. Re: What is assigning object inside the class using scope some varoutside the class is illegal?

    On Apr 15, 4:56 pm, Andrey Tarasevich <>
    wrote:
    > puzzlecracker wrote:
    > > Say we have the following class (adapted from the standard):

    >
    > > enum {i=1};
    > > class X{
    > > char v;
    > > enum{i=2} // why this is illegal?

    >
    > Huh? Aside from the missing semicolon, this is perfectly legal.
    >
    > > int f(){ return sizeof(c); } // and this is legal?

    >
    > What is legal? 'c' is not declared. How can it possibly be legal?
    >
    > --
    > Best regards,
    > Andrey Tarasevich


    Sorry, I meant different lines:

    typedef int c
    enum {i=1};
    class X{
    char v; // why this is illegal?
    enum{i=2};
    int f(){ return sizeof(c); } // and this is legal?

    };
     
    puzzlecracker, Apr 15, 2008
    #3
  4. Re: What is assigning object inside the class using scope some varoutside the class is illegal?


    >
    > > enum {i=1};
    > > class X{
    > > char v; // why this is illegal?

    >
    > Who says it's illegal?
    >


    C++ Standard 3.3.6
     
    puzzlecracker, Apr 15, 2008
    #4
  5. Re: What is assigning object inside the class using scope some varoutside the class is illegal?


    >
    > > enum {i=1};
    > > class X{
    > > char v; // why this is illegal?

    >
    > Who says it's illegal?
    >


    C++ Standard 3.3.6
     
    puzzlecracker, Apr 15, 2008
    #5
  6. Re: What is assigning object inside the class using scope some varoutside the class is illegal?


    >
    > > enum {i=1};
    > > class X{
    > > char v; // why this is illegal?

    >
    > Who says it's illegal?
    >


    C++ Standard 3.3.6
     
    puzzlecracker, Apr 15, 2008
    #6
  7. Re: What is assigning object inside the class using scope some varoutside the class is illegal?

    puzzlecracker wrote:
    > ...
    > Sorry, I meant different lines:
    >
    > typedef int c


    typedef int c;

    > enum {i=1};
    > class X{
    > char v; // why this is illegal?
    > enum{i=2};
    > int f(){ return sizeof(c); } // and this is legal?
    > };


    OK, I was wrong in my previous message. The declaration of this class is
    indeed illegal (although no diagnostic is required). As for why... Well,
    you said it yourself, it is illegal because the language standard says so.

    This is done that way to prevent users from making some rather
    unexpected and nasty errors. If you read the name look up rules of C++
    language (3.4.1), you'll find out that unqualified names used in class
    definition outside of member functions (like 'i' in your code) are not
    looked up in the entire class, but only in the "already-defined" portion
    of it. At one point inside the class definition a name might mean one
    thing and at some other point later it might get re-defined mean a
    completely different thing (which is what happens to 'i' in your code).
    This might lead to unintended results, if you, for example, change the
    order of the class member declarations.

    There's a detailed explanation of this design decision in D&E book.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Apr 15, 2008
    #7
  8. Re: What is assigning object inside the class using scope some varoutside the class is illegal?

    Victor Bazarov wrote:
    > Posting the same thing thrice does not make it any truer. Which
    > part of 3.3.6 makes the following code ill-formed?
    >
    > enum {i=1};
    > class X {
    > char v;
    > enum {i=2};
    > };


    3.3.6/1. Point 3) and the example after point 5).

    Comeau accepts the code, which is what misled me originally. Apparently
    it lazily took advantage of the permission to produce no diagnostic,
    given in 3).

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Apr 15, 2008
    #8
  9. puzzlecracker

    Ian Collins Guest

    Re: What is assigning object inside the class using scope some varoutside the class is illegal?

    Andrey Tarasevich wrote:
    > puzzlecracker wrote:
    >> ...
    >> Sorry, I meant different lines:
    >>
    >> typedef int c

    >
    > typedef int c;
    >
    >> enum {i=1};
    >> class X{
    >> char v; // why this is illegal?
    >> enum{i=2};
    >> int f(){ return sizeof(c); } // and this is legal?
    >> };

    >
    > OK, I was wrong in my previous message. The declaration of this class is
    > indeed illegal (although no diagnostic is required). As for why... Well,
    > you said it yourself, it is illegal because the language standard says so.
    >

    Although the diagnostics form a couple of compilers are helpful:

    CC /tmp/x.cc
    "/tmp/x.cc", line 5: Error: Redefining i after use in X.

    g++ /tmp/x.cc
    /tmp/x.cc:5: error: declaration of `i'
    /tmp/x.cc:2: error: changes meaning of `i' from `<anonymous enum> i'

    --
    Ian Collins.
     
    Ian Collins, Apr 15, 2008
    #9
  10. puzzlecracker

    James Kanze Guest

    Re: What is assigning object inside the class using scope some varoutside the class is illegal?

    On 15 avr, 23:10, "Victor Bazarov" <> wrote:
    > puzzlecracker wrote:
    > > [..]
    > > Sorry, I meant different lines:

    >
    > > enum {i=1};
    > > class X{
    > > char v; // why this is illegal?


    > Who says it's illegal?


    The standard. See §3.3.6/1, point 2: "A name N used in a class
    S shall refer to the same declaration in its context and when
    re-evaluated in the completed scope of S." In the context
    above, the name i refers to the enum at namespace scope. In the
    complete scope of the class, it refers to the enum defined later
    in the class.

    Given that the example he's quoting is taken precisely from this
    paragraph, and is given in order to demonstrate this point, it's
    rather hard to understand what he's questioning, though.

    > > enum{i=2};
    > > int f(){ return sizeof(c); } // and this is legal?


    > Sure, since 'c' is a synonym for 'int', it's the same as


    > int f(){ reutnr sizeof(int); }


    Now who's complaining about uncompilable code? "reutnr", of
    all things:).

    In the example that he's misquoting, this line is followed by
    one with:
    char c ;
    So we end up in a variant of the above. Except that in this
    case, the name in question ("c") is in a function body, and
    the evaluation of function bodies is always in the complete
    scope of the class. In the standard, there is a comment on this
    line saying that "c", here is "X::c" (which means that the
    function is guaranteed to return 1, and not sizeof(int)).

    > > };


    The real problem here seems to be that the original poster
    doesn't know how to copy/paste:).

    Seriously, of course, if he'd have copied accurately (the
    publicly available version of the standard is, or at least was,
    restricted, so that you couldn't copy/paste from it), including
    the comments, you'd have seen what was up immediately. But I'll
    admit that, seeing the original text, I don't understand what he
    doesn't understand about it. There are many places where the
    standard is less than clear, but this really isn't one of them.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 15, 2008
    #10
  11. Re: What is assigning object inside the class using scope some varoutside the class is illegal?

    Thank you, folks, really helpful explanation.

    I am in the process of getting back to my "C++ roots", revitalizing
    some of hibernating knowledge and concepts, or lack thereof. And as
    such, I've started with C++ Standard and moving on to other exciting
    parts of C++ -- that coupled with preparation to 2009. I still need to
    catch up with Boost and TR1. This newsgroup has catered well to my
    education and inspired confidence in me; hence my lingering confidence
    inspired me to start learning it again.

    Interestingly, I am not involve in C++ programming at professional
    level (though working in a software firm), but feel compelled to
    continue my C++ education. Perhaps at some point in the nearing future
    land a job in the area, though it is not why I am learning it.

    Best Regards,

    puzzlecracker.

    P.S. From the post history, you can see when my C++ learning has been
    stalled.
     
    puzzlecracker, Apr 16, 2008
    #11
    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. Paul Opal
    Replies:
    12
    Views:
    963
    Paul Opal
    Oct 11, 2004
  2. Stephan Hoffmann
    Replies:
    2
    Views:
    624
    Stephan Hoffmann
    Dec 11, 2005
  3. Roy Smith
    Replies:
    5
    Views:
    645
    Bo Persson
    May 11, 2006
  4. Blackie
    Replies:
    1
    Views:
    414
    Blackie
    Oct 25, 2007
  5. weston
    Replies:
    1
    Views:
    256
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page