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

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

Post by puzzlecrac » Thu, 17 Apr 2008 06:00:03


On Apr 15, 4:56 pm, Andrey Tarasevich < XXXX@XXXXX.COM >



Sorry, I meant different lines:

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

};
 
 
 

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

Post by Victor Baz » Thu, 17 Apr 2008 06:10:13


Why do you keep posting syntactically incorrect code? How can
legality of your program be judged if it's ill-formed due to
some other errors? Post the code you actually have a problem
with, not the code you invented.


Who says it's illegal?


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

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


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

 
 
 

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

Post by puzzlecrac » Thu, 17 Apr 2008 06:13:41


C++ Standard 3.3.6
 
 
 

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

Post by puzzlecrac » Thu, 17 Apr 2008 06:16:13


C++ Standard 3.3.6
 
 
 

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

Post by puzzlecrac » Thu, 17 Apr 2008 06:19:10


C++ Standard 3.3.6
 
 
 

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

Post by Victor Baz » Thu, 17 Apr 2008 06:22:12


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[i];
enum {i=2};
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
 
 

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

Post by Andrey Tar » Thu, 17 Apr 2008 06:35:48


typedef int c;


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 *** 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
 
 
 

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

Post by Andrey Tar » Thu, 17 Apr 2008 06:39:18


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
 
 
 

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

Post by Pete Becke » Thu, 17 Apr 2008 06:41:52

On 2008-04-15 17:22:12 -0400, "Victor Bazarov" < XXXX@XXXXX.COM > said:



The redefinition of 'i' after it was used. That's what bullet 2) in
3.3.6/1 refers to. C++ scanning is pretty much single-pass, and that
rule keeps it that way. The compiler doesn't have to figure out what
v[i] means after i was changed.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
 
 
 

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

Post by Ian Collin » Thu, 17 Apr 2008 06:42:45


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.
 
 
 

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

Post by James Kanz » Thu, 17 Apr 2008 07:38:47


The standard. See ?.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.




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: XXXX@XXXXX.COM
Conseils en informatique orient objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sard, 78210 St.-Cyr-l'ole, France, +33 (0)1 30 23 00 34
 
 
 

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

Post by puzzlecrac » Thu, 17 Apr 2008 08:30:08

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.