Boost logo

Boost :

Subject: Re: [boost] [outcome] Exception safety guarantees
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2017-05-29 08:28:52


Mere moments ago, quoth I:
> For types that do have a well-defined empty state (like most smart
> pointers) it can be safer to make assumptions that the pointer is now
> empty after being moved-from. (As long as you remember that it's up to
> both the method you're passing the object to and the object itself
> whether the move actually happens -- calling std::move by itself is a
> declaration of intent, not an action. That trips many people up at first.)

Another example (which I believe is correct, but perhaps I've missed
something):

If you move-from a std::vector, or any other STL container, you are
guaranteed that the vector is in a sane state which you can still call
any methods on. You are *not* guaranteed that the vector is empty
(although this is reasonably likely since that's a typical side-effect
of the performant implementation).

Importantly, this means that it's legal to move a vector, then clear()
it and add more items to it.

If you forget to clear it, then it's still legal (there's no memory
leaks or other UB) but it's implementation-defined whether you end up
adding to an empty vector or appending to the original one (or some
weird hybrid, though that's less likely), so it's probably not something
you should actually do.

You could probably call empty() to figure out which it was, but there's
usually not much point -- you generally just want to clear() it and move
on (pun somewhat intended).


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk