Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is assigning object inside the class using scope some varoutside the class is illegal?

Reply
Thread Tools

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

 
 
puzzlecracker
Guest
Posts: n/a
 
      04-15-2008
Say we have the following class (adapted from the standard):

enum {i=1};
class X{
char v[i];
enum{i=2} // why this is illegal?
int f(){ return sizeof(c); } // and this is legal?
};
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      04-15-2008
puzzlecracker wrote:
> Say we have the following class (adapted from the standard):
>
> enum {i=1};
> class X{
> char v[i];
> 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
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 4:56 pm, Andrey Tarasevich <(E-Mail Removed)>
wrote:
> puzzlecracker wrote:
> > Say we have the following class (adapted from the standard):

>
> > enum {i=1};
> > class X{
> > char v[i];
> > 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[i]; // why this is illegal?
enum{i=2};
int f(){ return sizeof(c); } // and this is legal?

};
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      04-15-2008

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

>
> Who says it's illegal?
>


C++ Standard 3.3.6

 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      04-15-2008

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

>
> Who says it's illegal?
>


C++ Standard 3.3.6

 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      04-15-2008

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

>
> Who says it's illegal?
>


C++ Standard 3.3.6

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      04-15-2008
puzzlecracker wrote:
> ...
> Sorry, I meant different lines:
>
> typedef int c


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


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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      04-15-2008
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[i];
> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-15-2008
Andrey Tarasevich wrote:
> puzzlecracker wrote:
>> ...
>> Sorry, I meant different lines:
>>
>> typedef int c

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

>
> 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-15-2008
On 15 avr, 23:10, "Victor Bazarov" <(E-Mail Removed)> wrote:
> puzzlecracker wrote:
> > [..]
> > Sorry, I meant different lines:

>
> > enum {i=1};
> > class X{
> > char v[i]; // 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
slicing - copying or assigning derived object to base object subramanian100in@yahoo.com, India C++ 1 04-22-2008 02:55 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
problem with auto_ptr<> and assigning derived class to base class object Stephan Hoffmann C++ 2 12-11-2005 06:04 PM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments