🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1474

Re: Defining members of class T within class T (_not_ recursive!)

#1474
From: Tom Payne
Date: Thu, 03 Aug 1995 20:52
65 lines
3185 bytes
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)
Technical Details
Message-ID: <3vrcu8$pci@galaxy.ucr.edu>
Path: arc1-ca.pugleaf.net!corvette.ucr.edu!thp
References: <950729200137682@wem.org> <3vofe6$3bs@galaxy.ucr.edu> <hbaker-0208951642250001@192.0.2.1>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!corvette.ucr.edu!thp
From: thp@PROBLEM_WITH_INEWS_DOMAIN_FILE (Tom Payne)
Newsgroups: comp.std.c++,comp.lang.c++
Subject: Re: Defining members of class T within class T (_not_ recursive!)
Followup-To: comp.std.c++,comp.lang.c++
Date: 3 Aug 1995 20:52:24 GMT
Organization: University of California, Riverside Department of Computer Science
Message-ID: <3vrcu8$pci@galaxy.ucr.edu>
References: <950729200137682@wem.org> <3vofe6$3bs@galaxy.ucr.edu> <hbaker-0208951642250001@192.0.2.1>
NNTP-Posting-Host: corvette.ucr.edu
X-Newsreader: TIN [version 1.2 PL2]