Why doesn't this C++0x code call the move constructor?

前端 未结 2 600
面向向阳花
面向向阳花 2021-01-19 04:25

For some reason, the following code never calls Event::Event(Event&& e)

Event a;
Event b;
Event temp;
temp = move(a);
a = move(b);
b = m         


        
2条回答
  •  旧时难觅i
    2021-01-19 05:08

    You are not using the move constructor. I think swap is implemented something like this

    Event a;
    Event b;
    
    Event temp(move(a)); // this one wants to use a move constructor
    a = move(b);
    b = move(temp);
    

    You want to use the move assignment operator, which doesn't exist in your code, so it falls back to the copy assignment operator.

提交回复
热议问题