Consider the following:
#include <set>
#include <map>
struct MyClass
{
MyClass(int i);
MyClass(MyClass const&) = delete;
~MyClass();
bool operator<(const MyClass& r) const { return v < r.v; }
int v;
};
void map_set_move_test()
{
std::set<MyClass> s1, s2;
std::map<int, MyClass> m1;
s1.emplace(123);
s2.insert(std::move(s1.extract(s1.begin())));
// This fails
m1.insert(std::move(std::make_pair(1, std::move(s2.extract(s2.begin()).value()))));
}
I used std::set::extract
to successfully move an element from std::set
to another std::set
, as in:
s2.insert(std::move(s1.extract(s1.begin())));
But the compiler doesn't allow moving the element to std::map
this way:
m1.insert(std::move(std::make_pair(1, std::move(s2.extract(s2.begin()).value()))));
Any help would be appreciated. Link to compiler explorer.