I\'ve been working on a parser for commands
(which are fancy wrappers around large arrays of data), and have a queue that unhandled commands reside on. If I nee
moving an object may leave it in an invalid state. It's invariants are no longer guaranteed. You would be safe popping it from a non-intrusive queue.
The std::move itself does nothing other than tell the compiler, that it can select a comm routine that takes an r-value.
A well written comm routine, would then steal the representation from the old object for the new object. For instance, just copy the pointers to the new object, and zero the pointers in the old object (that way the old object destructor won't destroy the arrays).
if comm is not overloaded to do this there will not be any benefit to std::mov.
Yes, this is perfectly safe:
std::queue<T> q;
// add stuff...
T top = std::move(q.front());
q.pop();
pop()
doesn't have any preconditions on the first element in the q
having a specified state, and since you're not subsequently using q.front()
you don't have to deal with that object being invalidated any more.
Sounds like a good idea to do!
It depends on what the move constructor for your type does. If it leaves the original object in a state that can safely be destroyed, then all is well. If not, then you may be in trouble. Note that the comments about preconditions and valid states are about constraints on types defined in the standard library. Types that you define do not have those constraints, except to the extent that they use types from the standard library. So look at your move constructor to sort out what you can and can't do with a moved-from object.
Yes. As long as your std::queue
's container template argument ensures that there are no preconditions on the state of its contained values for pop_front()
; the default for std::queue
is std::deque
and that offers the guarantee.
As long as you ensure what I wrote on the previous paragraph, you are completely safe. You're about to remove that item from your queue, thus there is no reason not to move it out since you are taking ownership of that object.