Topic: Throwing exception from move constructor


Author: "george.ryan@gmail.com" <george.ryan@gmail.com>
Date: Tue, 26 May 2009 16:37:02 CST
Raw View
I reading the unique_ptr specification from N2857 I found "The move
constructor of D shall not throw an exception." which got me thinking:

What happens if one of my objects throws an exception from within a
move constructor during a push_back() operation? (Say for example, a
vector.) Does the vector still hold true with the promise of the
container requirements that the push_back() operation has no effect?
If we are moving memory, will the compiler be smart enough to put my
stuff back? :-)

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: wasti.redl@gmx.net
Date: Wed, 27 May 2009 10:36:42 CST
Raw View
On May 27, 12:37 am, "george.r...@gmail.com" <george.r...@gmail.com>
wrote:
> I reading the unique_ptr specification from N2857 I found "The move
> constructor of D shall not throw an exception." which got me thinking:
>
> What happens if one of my objects throws an exception from within a
> move constructor during a push_back() operation? (Say for example, a
> vector.) Does the vector still hold true with the promise of the
> container requirements that the push_back() operation has no effect?
> If we are moving memory, will the compiler be smart enough to put my
> stuff back? :-)

No. The point is that if one move to the target fails, how can you
gurantee that moving the already-moved objects back to the source
won't fail too?

A throwing move constructor is like a throwing destructor. If you have
one, you're screwed.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: sdt <sjdutoit@gmail.com>
Date: Wed, 27 May 2009 20:32:34 CST
Raw View
On May 26, 6:37 pm, "george.r...@gmail.com" <george.r...@gmail.com>
wrote:
> What happens if one of my objects throws an exception from within a
> move constructor during a push_back() operation? (Say for example, a
> vector.) Does the vector still hold true with the promise of the
> container requirements that the push_back() operation has no effect?
> If we are moving memory, will the compiler be smart enough to put my
> stuff back? :-)

Have a look at:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2855.html

which discusses exactly the example you provide :) (see "The Problem
With Throwing Move Constructors"). The short answer is: don't throw
exceptions from move constructors.

Stefanus


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "george.ryan@gmail.com" <george.ryan@gmail.com>
Date: Wed, 27 May 2009 20:32:45 CST
Raw View
On May 27, 1:36 pm, wasti.r...@gmx.net wrote:

> No. The point is that if one move to the target fails, how can you
> gurantee that moving the already-moved objects back to the source
> won't fail too?

Well, that was kinda my overall point.

I did some more reading, and for vectors I did find:

n2857 23.3.6.4
1 Requires: If value_type has a move constructor, that constructor
shall not throw any exceptions.

Which answered my question. :-)


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: google@dalvander.com
Date: Fri, 29 May 2009 08:26:44 CST
Raw View
On May 28, 4:32 am, sdt <sjdut...@gmail.com> wrote:
> Have a look at:
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2855.html
>
> which discusses exactly the example you provide :) (see "The Problem
> With Throwing Move Constructors"). The short answer is: don't throw
> exceptions from move constructors.
>
> Stefanus

As compound classes (such as std::pair) with move constructors cannot
detect if the compounded classes has nothrowing move constructors, the
long answer seems to be: "don't throw exceptions from copy
constructors either". Unfortunately N2855 doesn't solve this issue in
a non-breaking way.

Regards,
Anders Dalvander


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]