lundi 18 septembre 2017

std::set::emplace complaining about deleted copy constructor

Suppose I have the following class in a header file:

//Minimized example to show the issue
class Foo    
{
public:
    Foo(); //implemented in .cpp
    Foo(Foo const &foo) = delete;
    Foo(Foo &&foo); //implemented in .cpp
private:
    //member variables
}

I wrote a function that takes in Foo && as a parameter and emplaces it to a set. Which looks like this:

void bar(Foo &&foo)
{
    //std::set<Foo> mySet defined elsewhere
    mySet.emplace(foo); //Compiler complains using deleted copy ctor
}

The compiler would complain my code was using the =delete'd copy ctor, but if I change the function body to:

mySet.emplace(std::move(foo));

everything becomes fine.

Why does calling std::move on foo solve this problem?

I'm using Windows 10 and VS 2017.

I found std::unordered_map::emplace issue with private/deleted copy constructor but that was using gcc and might be outdated.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire