0

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.

user3291342
  • 101
  • 2
  • 9
  • And a copy of `foo` becomes type `Foo &`...? – user3291342 Sep 19 '17 at 00:52
  • re-reading, I realize it's not clear at all because I had not looked at your definition of Foo, so I'll try to rephrase: within `bar`, `foo` is an lvalue, and is not eligible for `Foo(Foo&&)`. So the compiler picks the other and complains it's deleted. – spectras Sep 19 '17 at 01:02
  • @spectras so you are saying with `bar`, `foo` is an lvalue, and an rvalue reference? – user3291342 Sep 19 '17 at 01:04
  • @user3291342 the entity `foo` is an rvalue reference, but the expression `foo` is an lvalue of type `Foo` – M.M Sep 19 '17 at 01:07

0 Answers0