Topic: Defining members of class T within class T (_not_ recursive!)
Author: thp@PROBLEM_WITH_INEWS_DOMAIN_FILE (Tom Payne)
Date: 1995/08/02 Raw View
Henry Baker (henry.baker@wem.org) wrote:
: Suppose I have the following nested class:
: struct pair
: {struct pair_rep
: {pair car,cdr;}; // inner class has member of outer class.
: pair_rep *rep;};
: I understand the rule saying that a class cannot be a member of itself
: [Stroustrup#2, page 545] (Bertrand Russell is now spinning in his grave ;-),
: but I'm having trouble understanding why a _nested_ class cannot have a
: member
: of the outer class being defined. This is especially irritating, since the
: above is capable of being rewritten as [Stroustrup#2, page 645]:
: struct pair
: {struct pair_rep;
: pair_rep *rep;};
: struct pair::pair_rep
: {pair car,cdr;};
: Are compiler writers just being lazy, or what??
It appears that the struct type pair is not yet defined at the point
where it is used to delcare car and cdr. (Note that sizeof(pair) will
not be known for another two lines.)
In the revised version, when the struct type pair is used to declare
car and cdr, it is already known that pairs consist of a single member
called rep, which points to a structure of a thus-far undefined type
called pair_rep.
Tom Payne (thp@cs.ucr.edu)
Author: hbaker@netcom.com (Henry Baker)
Date: 1995/08/03 Raw View
In article <3vofe6$3bs@galaxy.ucr.edu>, thp@PROBLEM_WITH_INEWS_DOMAIN_FILE
(Tom Payne) wrote:
> Henry Baker (henry.baker@wem.org) wrote:
> : Suppose I have the following nested class:
>
> : struct pair
> : {struct pair_rep
> : {pair car,cdr;}; // inner class has member of outer class.
> : pair_rep *rep;};
>
> : I understand the rule saying that a class cannot be a member of itself
> : [Stroustrup#2, page 545] (Bertrand Russell is now spinning in his grave ;-),
> : but I'm having trouble understanding why a _nested_ class cannot have a
> : member
> : of the outer class being defined. This is especially irritating, since the
> : above is capable of being rewritten as [Stroustrup#2, page 645]:
>
> : struct pair
> : {struct pair_rep;
> : pair_rep *rep;};
>
> : struct pair::pair_rep
> : {pair car,cdr;};
>
> : Are compiler writers just being lazy, or what??
>
>
> It appears that the struct type pair is not yet defined at the point
> where it is used to delcare car and cdr. (Note that sizeof(pair) will
> not be known for another two lines.)
>
> In the revised version, when the struct type pair is used to declare
> car and cdr, it is already known that pairs consist of a single member
> called rep, which points to a structure of a thus-far undefined type
> called pair_rep.
So should I put down your vote in the 'lazy compiler writers' column?
I didn't hear any good reasons for not accepting the code as originally
written.
--
www/ftp directory:
ftp://ftp.netcom.com/pub/hb/hbaker/home.html
Author: thp@PROBLEM_WITH_INEWS_DOMAIN_FILE (Tom Payne)
Date: 1995/08/03 Raw View
Henry Baker (hbaker@netcom.com) wrote:
: In article <3vofe6$3bs@galaxy.ucr.edu>, thp@PROBLEM_WITH_INEWS_DOMAIN_FILE
: (Tom Payne) wrote:
: > Henry Baker (henry.baker@wem.org) wrote:
: > : Suppose I have the following nested class:
: >
: > : struct pair
: > : {struct pair_rep
: > : {pair car,cdr;}; // inner class has member of outer class.
: > : pair_rep *rep;};
: >
: > : I understand the rule saying that a class cannot be a member of itself
: > : [Stroustrup#2, page 545] (Bertrand Russell is now spinning in his grave ;-),
: > : but I'm having trouble understanding why a _nested_ class cannot have a
: > : member
: > : of the outer class being defined. This is especially irritating, since the
: > : above is capable of being rewritten as [Stroustrup#2, page 645]:
: >
: > : struct pair
: > : {struct pair_rep;
: > : pair_rep *rep;};
: >
: > : struct pair::pair_rep
: > : {pair car,cdr;};
: >
: > : Are compiler writers just being lazy, or what??
: >
: >
: > It appears that the struct type pair is not yet defined at the point
: > where it is used to delcare car and cdr. (Note that sizeof(pair) will
: > not be known for another two lines.)
: >
: > In the revised version, when the struct type pair is used to declare
: > car and cdr, it is already known that pairs consist of a single member
: > called rep, which points to a structure of a thus-far undefined type
: > called pair_rep.
: So should I put down your vote in the 'lazy compiler writers' column?
: I didn't hear any good reasons for not accepting the code as originally
: written.
I'm voting "overworked compiler writers."
C++ does not use C's single-pass semantics for names occurring within
a class. Specifically, the C++ "reconsideration rule" states:
A name used in a class S must refer to the same declaration when
reevaluated in its context and in the completed scope of S, which
consists of the class S, S's base classes, and all classes enclosing
S. [D&E, p.142]
This seems to indicate that the occurrence of the name pair in the
declaration of pair_rep should refer to the completed declaration
of pair rather than to the portion processed up to that point.
D&E indicates that with sufficient post processing of the symbol-
table data structures, this rule can be implemented in a single-pass
translator. That is, the compiler is expected to complete the
processing of the declaration of cdr, car, and pair_rep after it
has completed the processing of the declaration of pair.
Tom Payne (thp@cs.ucr.edu)
Author: hbaker@netcom.com (Henry Baker)
Date: 1995/07/29 Raw View
Suppose I have the following nested class:
struct pair
{struct pair_rep
{pair car,cdr;}; // inner class has member of outer class.
pair_rep *rep;};
I understand the rule saying that a class cannot be a member of itself
[Stroustrup#2, page 545] (Bertrand Russell is now spinning in his grave ;-),
but I'm having trouble understanding why a _nested_ class cannot have a member
of the outer class being defined. This is especially irritating, since the
above is capable of being rewritten as [Stroustrup#2, page 645]:
struct pair
{struct pair_rep;
pair_rep *rep;};
struct pair::pair_rep
{pair car,cdr;};
Are compiler writers just being lazy, or what??
--
www/ftp directory:
ftp://ftp.netcom.com/pub/hb/hbaker/home.html
Author: henry.baker@wem.org (Henry Baker)
Date: 1995/07/29 Raw View
Path: holonet!colossus.holonet.net!news.sprintlink.net!gatech!news.uoregon.
edu!news.bc.net!info.ucla.edu!library.ucla.edu!csulb.edu!csus.edu!
netcom.com!NewsWatcher!user
From: hbaker@netcom.com (Henry Baker)
Subject: Defining members of class T within class T (_not_ recursive!)
Message-ID: <hbaker-2907951547200001@192.0.2.1>
Sender: hbaker@netcom9.netcom.com
Organization: nil
Date: Sat, 29 Jul 1995 23:47:20 GMT
Suppose I have the following nested class:
struct pair
{struct pair_rep
{pair car,cdr;}; // inner class has member of outer class.
pair_rep *rep;};
I understand the rule saying that a class cannot be a member of itself
[Stroustrup#2, page 545] (Bertrand Russell is now spinning in his grave ;-),
but I'm having trouble understanding why a _nested_ class cannot have a
member
of the outer class being defined. This is especially irritating, since the
above is capable of being rewritten as [Stroustrup#2, page 645]:
struct pair
{struct pair_rep;
pair_rep *rep;};
struct pair::pair_rep
{pair car,cdr;};
Are compiler writers just being lazy, or what??
--
www/ftp directory:
ftp://ftp.netcom.com/pub/hb/hbaker/home.html
Author: henry.baker@wem.org (Henry Baker)
Date: 1995/07/29 Raw View
Path: holonet!colossus.holonet.net!news.sprintlink.net!gatech!news.uoregon.
edu!news.bc.net!info.ucla.edu!library.ucla.edu!csulb.edu!csus.edu!
netcom.com!NewsWatcher!user
From: hbaker@netcom.com (Henry Baker)
Subject: Defining members of class T within class T (_not_ recursive!)
Message-ID: <hbaker-2907951547200001@192.0.2.1>
Sender: hbaker@netcom9.netcom.com
Organization: nil
Date: Sat, 29 Jul 1995 23:47:20 GMT
Suppose I have the following nested class:
struct pair
{struct pair_rep
{pair car,cdr;}; // inner class has member of outer class.
pair_rep *rep;};
I understand the rule saying that a class cannot be a member of itself
[Stroustrup#2, page 545] (Bertrand Russell is now spinning in his grave ;-),
but I'm having trouble understanding why a _nested_ class cannot have a
member
of the outer class being defined. This is especially irritating, since the
above is capable of being rewritten as [Stroustrup#2, page 645]:
struct pair
{struct pair_rep;
pair_rep *rep;};
struct pair::pair_rep
{pair car,cdr;};
Are compiler writers just being lazy, or what??
--
www/ftp directory:
ftp://ftp.netcom.com/pub/hb/hbaker/home.html