title: Cost of std::vector::push_back either succeeding or having no effect?
body: <p>If I understand correctly, <a href="http://en.cppreference.com/w/cpp/container/vector/insert" rel="nofollow"><code>std::vector::insert</code></a> does not guarantee a commit-or-rollback for <a href="http://en.cppreference.com/w/cpp/container/vector" rel="nofollow"><code>std::vector</code></a>s (it does for <code>std::list</code>s for obvious reasons) in the case an exception is thrown during copying or moving, because of the high cost of checking for exceptions. I recently saw that <code>push_back</code> DOES guarantee either successful insertion at the end or nothing happens. </p>
  
  <p>My question is the following. Suppose that during a <code>push_pack</code> the vector has to be resized (reallocation happens). In that case, all elements have to be copied in the new container, via copy or move semantics. Assuming that the move constructor is not guaranteed <code>noexcept</code>, <code>std::vector</code> will then use copy semantics. So, to guarantee the above behaviour of <code>push_pack</code>, <code>std::vector</code> has to check for successful copying, and if not, roll back via a swap the initial vector. Is this what is happening, and if so, isn't this expensive? Or, because reallocation happens rarely, one can say that the amortized cost is low?</p>