lundi 26 juin 2017

std::remove, std::move(range) and moved-from elements

Looking at std::remove reference I see:

template< class ForwardIt, class T >
ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );

Removing is done by shifting (by means of move assignment) the elements in the range. Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as per MoveAssignable post-condition).

So, I see MoveAssignable conept:

t = rv   // Post-condition:The new value of rv is unspecified.

So far, it is OK. Question about MoveAssignable concept:

std::string s1 = "abc";
std::string s2;
s2 = std::move(s1);
std::cout << s1;    // is this code valid?
s1 = "cde";         // is this code valid?

Can I reuse "moved from" variable by reading its value or by re-assigning its value?

Now I am looking at std::move reference and it looks a bit surprising:

template< class InputIt, class OutputIt >
OutputIt move( InputIt first, InputIt last, OutputIt d_first );

Moves the elements in the range [first, last), to another range beginning at d_first. After this operation the elements in the moved-from range will still contain valid values of the appropriate type, but not necessarily the same values as before the move.

So, moved-from elements are defined by different way in std::remove and std::move, though they should be the same. Which one is correct? What are the rules of reusing (or not using) of moved-from elements?

Aucun commentaire:

Enregistrer un commentaire