Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to move an item out of a std::set?

Tags:

c++

c++11

If I have an object that only allows move-only semantics - is it possible to move items from a set? I can't seem to find a way to do this.

like image 767
sircodesalot Avatar asked Aug 26 '14 21:08

sircodesalot


People also ask

What does std :: move () do?

std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object. In particular, std::move produces an xvalue expression that identifies its argument t . It is exactly equivalent to a static_cast to an rvalue reference type.

What happens to object after std :: move?

std::move is actually just a request to move and if the type of the object has not a move constructor/assign-operator defined or generated the move operation will fall back to a copy.

Where is std :: move defined?

Move Constructor And Semantics: std::move() is a function used to convert an lvalue reference into the rvalue reference. Used to move the resources from a source object i.e. for efficient transfer of resources from one object to another. std::move() is defined in the <utility> header.


1 Answers

No, it is not possible. There is no way to get non-const access to elements in a set, and move requires non-const references. Allowing non-const access would make it trivially easy to break the invariants for set.

like image 171
Nevin Avatar answered Oct 05 '22 03:10

Nevin