🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: an optimization I claim is illegal

#2663
From: Steve Clamage
Date: Mon, 20 Nov 1995 16:34
116 lines
3471 bytes
James Kanze (kanze@gabi-soft.fr) wrote:
>Alex Martelli (martelli@cadlab.it) wrote:
>
>: In a debate over on comp.compilers, there was some discussion of
>: whether it would be legal for a compiler to optimize the following
>: snippet of code...:
>
>: class P {
>: public: virtual void f();
>:     // etc
>: };
>
>: // with a P* p
>:     for(i=0;i<1000;i++)
>: 	p->f();
>
>: by hoisting out of the loop the virtual lookup for f()'s address.
>

[code removed, including Derived class deriving from Base class]

>
>There's one obvious problem.  The address of Base in Derived is not
>guaranteed to be the same as the address of Derived.


Tim Hollebeek also wrote:

[snip again]

> I think it's pretty clear the answer is no:
> 
> [class.dtor] #11:
> 
> "...  Once a destructor is invoked for an object, the object no longer
>  exists; ..."
> 
> Now, it is legal to use a (T*) as a (void*) even after the T's life
> has ended, however:
> 
> [basic.life] #7:
> 
> "If, after the lifetime of an object has ended and while the storage
> which the object occupied still exists, a new object is created at the
> storage location which the original object occupied, a pointer that
> pointed to the original object will automatically refer to the new
> object and, once the lifetime of the new object has started, can be
> used to manipulate the new object, if:
> 
> [...]
> 
> ... the new object is of THE SAME TYPE as the original object ...
> 
> [...]
> 

What is meant by the SAME TYPE in the piece of the Draft quoted ?  Would
the following code be legal:

-- start here --

#include <iostream.h>
#include <defalloc.h>

struct Base
{
    int i;
    Base (int val) : i(val) {};
    Base () : i(i) {};
    virtual ~Base () {};
    virtual void f () { cout << "Base" << endl; };
};

struct D2;

struct D1 : public Base
{
    D1 (int val) : Base(val) {};
	D1 () : Base () {};
    virtual ~D1 () {};
    virtual void f ();
};

struct D2 : public Base
{
    D2 (int val) : Base(val) {};
	D2 () : Base () {};
    virtual ~D2 () {};
    virtual void f ();
};

void D1::f () { cout << "D1" << endl; new (this) D2 (); }
void D2::f () { cout << "D2" << endl; new (this) D1 (); }

int main (void)
{
    D1 d1 (7);
    Base *pb = &d1;
    for (int i = 0; i < 5; i++)
        pb->f();
	return 0;
}

-- end here --

The output on g++ is 'D1 D2 D1 D2 D1'. The type as seen by the loop
in main does not change, the layout of the types D1 and D2 are the
same, they have the same parents and the same function. Why would it
be illegal in this case ?  I do not ask this for purely theoretical
reason: I do use this (in a somewhat more involved ways) in a design
of networked pointers.


[ 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: <Pine.A32.3.91.951120093534.26414A-100000@zorglub.cae.ca>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: clamage@taumet.eng.sun.com (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: an optimization I claim is illegal
Date: 20 Nov 1995 16:34:58 GMT
Organization: ?
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <Pine.A32.3.91.951120093534.26414A-100000@zorglub.cae.ca>
NNTP-Posting-Host: taumet.eng.sun.com
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset="US-ASCII"
X-Lines: 112
Content-Length: 2661