An interesting question was asked recently on StackOverflow that nicely ties in with Scott Meyers' "Effective C++11/14 Sampler" talk two months ago at GoingNative 2013 and the interestingly named feature std::move_if_noexcept
, both referenced in the answers.
Since the gorier details of the answer lie in "watch Scott's talk," we merrily abuse a snapshot of Dr. Meyers during said talk as the highlight graphic for this post.
Exception safe code and move semantics
I want to write container class. This container has insert method that have two specializations -- first uses copy constructors to copy data from one container to another container element wise. If copy constructor throws exception I just undo all changes to the container like nothing happens.
The second specialization uses move constructor and thats the place where things got complicated. When I move items from one container to another container element by element, move constructor can throw exception. If this happens -- I've got really messy state when some elements are moved and other elements stays in it's original places. If I try to move elements back -- I can get another exception.
Is it possible to write something like this in exception safe manner or exception safety and move semantics are mutually exclusive?
Add a Comment
Comments are closed.