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

P

puzzlecracker

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?
};
 
A

Andrey Tarasevich

puzzlecracker said:
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?
 
P

puzzlecracker

puzzlecracker said:
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?


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?

};
 
A

Andrey Tarasevich

puzzlecracker said:
...
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.
 
A

Andrey Tarasevich

Victor said:
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).
 
I

Ian Collins

Andrey said:
puzzlecracker said:
...
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'
 
J

James Kanze

puzzlecracker said:
[..]
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.
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.
 
P

puzzlecracker

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top