What is the context of move constructors leaving an object in a valid but unspecified state, and does it apply to potential out-of-memory situations with std::vector::push_back?
No, it doesn't apply. Having enough memory to do a push_back
is not a precondition. It's OK to call push_back
when the system has no more memory (in general it's not possible for the program to know in advance if the allocation will succeed or not) and if there isn't enough memory you get an exception. That's just the normal behaviour of push_back
not a precondition violation.
In particular, if std::vector::push_back would have worked prior to a move, is it guaranteed to work after (ignoring issues such as other processes using up memory)?
It's legal to attempt to push_back
but it's not guaranteed to work. After a move the vector's storage might have been moved to the target of the move, and push_back
could cause a reallocation which could fail and throw bad_alloc
.
But ignoring allocation failure, the push_back
will succeed, and as Howard's comment says, now your vector which previously had an unknown number of elements has an unknown number plus one. Valid but not very useful.