Article View: comp.std.c++
Article #2698Re: an optimization I claim is illegal
From: Dick Menninger
Date: Wed, 22 Nov 1995 02:46
Date: Wed, 22 Nov 1995 02:46
83 lines
3300 bytes
3300 bytes
> ==========Somebody said, 11/20/95==========
> 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.
Wow. This code raises several questions. As coded,
the "object" goes through several ctors as it changes
"personality" without ever a dtor called. Is that legal?
Is that reasonable? That seems to make this a different
problem as you overwrite a living instance through which
the function being executed was called instead of
first destroying it and then creating a new one over
the ashes of the old (or am I confusing two threads).
Is there any requirement that the actual underlying
images be the same size? Can't the compiler could have
other info hidden in the image that could even affect
the offset of this in the memory block used and still
be a conforming compiler? Couldn't that info even be
stuff that could go insane if you did this (list linkage
for various debugging support)?
Good Day
Dick
Dick.Menninger@DaytonOH.ATTGIS.COM
---
[ 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:
<DIF0Hw.FrE@falcon.daytonoh.attgis.com>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<Pine.A32.3.91.951120093534.26414A-100000@zorglub.cae.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 22 Nov 95 02:46:40 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ From: Dick Menninger <Dick.Menninger@daytonoh.attgis.com> Subject: Re: an optimization I claim is illegal Message-ID: <DIF0Hw.FrE@falcon.daytonoh.attgis.com> Reply-To: mennid <Dick.Menninger@daytonoh.attgis.com> Organization: AT&T Global Information Solutions X-Newsreader: DiscussIT 2.5.1.3 for MS Windows [AT&T Software Products Division] References: <Pine.A32.3.91.951120093534.26414A-100000@zorglub.cae.ca> X-Original-Date: Tue, 21 Nov 1995 22:44:20 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMLKPLOEDnX0m9pzZAQGJGwF/VuQixfNJjtGM1AxcErY+nnBVA2SKeT1v KAlNTYWs8lzIJblFdDtEGBl/5Li1Cpey =27PJ