Topic: Reconstructing objects...
Author: dave@boost-consulting.com (David Abrahams)
Date: Wed, 11 Dec 2002 18:51:35 +0000 (UTC) Raw View
Herb Sutter <hsutter@gotw.ca> writes:
> On Mon, 9 Dec 2002 20:23:44 +0000 (UTC), hyrosen@mail.com (Hyman Rosen)
> wrote:
>>Mirek Fidler wrote:
>>> There is nothing about possible reconstruction... It would be nice if
>>> future ISO C++ clarified this issue.
>>
>>Present C++ addresses this explicitly, in 3.8/8. It's fine.
>
> Hyman is correct, although there is language that you can't do it for const
> objects (3.8/9).
>
> For some discussion of this destroy-and-reconstruct issue, see also Items 40
> and 41 of my first book Exceptional C++. Item 40 features nearly the very
> function you ask about:
>
> void T::DestroyAndReconstruct( int i )
> {
> this->~T();
> new (this) T(i);
> }
A very bad idea for objects whose constructors can throw, BTW! You
definitely don't want to make a template like this, unless you're very
careful about how it's used.
--
David Abrahams
dave@boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: hsutter@gotw.ca (Herb Sutter)
Date: Thu, 12 Dec 2002 01:02:16 +0000 (UTC) Raw View
On Wed, 11 Dec 2002 18:51:35 +0000 (UTC), dave@boost-consulting.com (David
Abrahams) wrote:
>Herb Sutter <hsutter@gotw.ca> writes:
[...]
>> For some discussion of this destroy-and-reconstruct issue, see also Items 40
>> and 41 of my first book Exceptional C++. Item 40 features nearly the very
>> function you ask about:
>>
>> void T::DestroyAndReconstruct( int i )
>> {
>> this->~T();
>> new (this) T(i);
>> }
>
>A very bad idea for objects whose constructors can throw, BTW! You
>definitely don't want to make a template like this, unless you're very
>careful about how it's used.
Indeed. As I'm sure Dave knows, that's one of the points I made in the
article references that followed. :-) There are other problems with this
approach, however, that I'd argue are at least as bad.
Herb
---
Herb Sutter (www.gotw.ca)
Convener, ISO WG21 - Secretary, ANSI J16 (www.gotw.ca/iso)
Contributing editor, C/C++ Users Journal (www.gotw.ca/cuj)
C++ community program manager, Microsoft (www.gotw.ca/microsoft)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Herb Sutter <hsutter@gotw.ca>
Date: Wed, 11 Dec 2002 10:08:33 CST Raw View
On Mon, 9 Dec 2002 20:23:44 +0000 (UTC), hyrosen@mail.com (Hyman Rosen)
wrote:
>Mirek Fidler wrote:
>> There is nothing about possible reconstruction... It would be nice if
>> future ISO C++ clarified this issue.
>
>Present C++ addresses this explicitly, in 3.8/8. It's fine.
Hyman is correct, although there is language that you can't do it for const
objects (3.8/9).
For some discussion of this destroy-and-reconstruct issue, see also Items 40
and 41 of my first book Exceptional C++. Item 40 features nearly the very
function you ask about:
void T::DestroyAndReconstruct( int i )
{
this->~T();
new (this) T(i);
}
Earlier versions of that material are also available online at
www.gotw.ca/gotw/022.htm and www.gotw.ca/gotw/023.htm.
Herb
---
Herb Sutter (www.gotw.ca)
Convener, ISO WG21 - Secretary, ANSI J16 (www.gotw.ca/iso)
Contributing editor, C/C++ Users Journal (www.gotw.ca/cuj)
C++ community program manager, Microsoft (www.gotw.ca/microsoft)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: cxl@volny.cz ("Mirek Fidler")
Date: Mon, 9 Dec 2002 01:10:49 +0000 (UTC) Raw View
It there any chance that folowing template is legal (I am afraid it is not)
and that it could be defined in future C++ 0x ? (As in fact it is hard to
imagine platform where it would fail).
template <class T>
void Reconstruct(T& object)
{
object.T::~T();
new(&object) T;
}
void foo()
{
std::string q;
Reconstruct(q);
}
(it expects T to be default constructible).
Mirek
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: petebecker@acm.org (Pete Becker)
Date: Mon, 9 Dec 2002 09:37:06 +0000 (UTC) Raw View
Mirek Fidler wrote:
>
> It there any chance that folowing template is legal (I am afraid it is not)
> and that it could be defined in future C++ 0x ? (As in fact it is hard to
> imagine platform where it would fail).
>
> template <class T>
> void Reconstruct(T& object)
> {
> object.T::~T();
> new(&object) T;
> }
>
> void foo()
> {
> std::string q;
> Reconstruct(q);
> }
>
> (it expects T to be default constructible).
>
struct Base
{
virtual void f();
};
struct Derived : public Base
{
void f();
};
void surprise(Base& b)
{
Reconstruct(b);
}
Derived d;
surprise(d);
d.f(); // surprise: now calls Base::f instead of Derived::f
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Mon, 9 Dec 2002 09:37:19 +0000 (UTC) Raw View
Mirek Fidler wrote:
> It there any chance that folowing template is legal
It's fine. There are uses that can get you into trouble,
but only as a consequence of doing other illegal things,
not from this template itself.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: cxl@volny.cz ("Mirek Fidler")
Date: Mon, 9 Dec 2002 18:28:11 +0000 (UTC) Raw View
> > It there any chance that folowing template is legal (I am afraid it is
not)
> > and that it could be defined in future C++ 0x ? (As in fact it is hard
to
> > imagine platform where it would fail).
> >
> > template <class T>
> > void Reconstruct(T& object)
> > {
> > object.T::~T();
> > new(&object) T;
> > }
> >
> > void foo()
> > {
> > std::string q;
> > Reconstruct(q);
> > }
> >
> > (it expects T to be default constructible).
> >
>
> struct Base
> {
> virtual void f();
> };
>
> struct Derived : public Base
> {
> void f();
> };
>
> void surprise(Base& b)
> {
> Reconstruct(b);
> }
>
> Derived d;
> surprise(d);
> d.f(); // surprise: now calls Base::f instead of Derived::f
Of course it does. But I was not asking about pitfalls of Reconstruct,
just about possible standard conformance. BTW:
Derived *d = new Derived;
surprise(*d);
has same problem, but until this point stays in perfectly defined land
by ISO C++ AFAIK.
Mirek
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: cxl@volny.cz ("Mirek Fidler")
Date: Mon, 9 Dec 2002 18:28:28 +0000 (UTC) Raw View
> > It there any chance that folowing template is legal
>
> It's fine. There are uses that can get you into trouble,
> but only as a consequence of doing other illegal things,
> not from this template itself.
I am not sure if it is possible to use it for automatic variable.
12.4.14 says
Once a destructor is invoked for an object, the object no longer
exists; the behavior is undefined if the destructor is invoked for an
object whose lifetime has ended. [Example: if the
destructor for an automatic object is explicitly invoked, and the
block is subsequently left in a manner that would ordinarily invoke
implicit destruction of the object, the behavior is undefined. ]
There is nothing about possible reconstruction... It would be nice if
future ISO C++ clarified this issue.
Mirek
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Mon, 9 Dec 2002 20:23:44 +0000 (UTC) Raw View
Mirek Fidler wrote:
> There is nothing about possible reconstruction... It would be nice if
> future ISO C++ clarified this issue.
Present C++ addresses this explicitly, in 3.8/8. It's fine.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]