🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: an optimization I claim is illegal

#2609
From: Alex Martelli
Date: Fri, 17 Nov 1995 23:18
53 lines
2954 bytes
clamage@Eng.Sun.COM (Steve Clamage) writes:
	...
>The question is whether an object can change type during execution of a
>member function other than during construction or destruction.

I think it can -- but if it does then pointers to the original
object can only be used in very restricted ways... NOT including
calling a method through them.  In other words, I was wrong, and
the only (quite minor) issue is whether section 3.8 is clear
enough on this subject (an editorial matter).  (I think it may
not be, since it seems to me that I'm not the only one not to
be crystal-clear on this admittedly obscure subject:-).

>You can allocate raw storage, construct an object in it, destroy the
>object, and construct a new object of different type at the same location.
>The key point is whether you can do that during execution of a member
>function on behalf of the original object.

>I think the answer is no.

Yet that is what the (non-normative, I know...) examples in 3.8 are
doing, e.g. in verse 6 (mutate is a member function of class B,
f a virtual member function thereof, D2 a derived class [no larger
than B, one assumes?] that overrides f):

void B::mutate() {
      new (this) D2;  // reuses storage - ends the lifetime of '*this'
      f();            // undefined behavior
      ... = this;     // ok, 'this' points to valid memory
}

Isn't this meant to work if one comments out the call to f()?  Yet
it is "ending the lifetime of *this" during execution of a member
function thereof.

But, regarding the optimization which I originally claimed was
illegal, I was still wrong, because -- while apparently you *can*
change an object's type during the execution of one of its member
functions -- if you do so then you cannot do anything much from
pointers to it (including 'this').


Alex

--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <martelli.816646978@cadlab>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <48ge05$h00@engnews1.eng.sun.com> <48gmat$i43@engnews1.eng.sun.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: martelli@cadlab.it (Alex Martelli)
Newsgroups: comp.std.c++
Subject: Re: an optimization I claim is illegal
Date: 17 Nov 1995 23:18:22 GMT
Organization: Cad.Lab Spa, Bologna, Italia
Sender: news@cadlab.it
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <martelli.816646978@cadlab>
References: <48ge05$h00@engnews1.eng.sun.com> <48gmat$i43@engnews1.eng.sun.com>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Newsreader: NN version 6.5.0 #11
Content-Length: 2108
Originator: clamage@taumet