为什么不调用移动分配?

时间:2018-03-05 11:34:36

标签: c++11 move-semantics

#include <iostream>

using namespace std;

struct Item
{
  Item () {cout << "Item constructor called." << endl;}
  ~Item () {cout << "Item destructor called." << endl;}
  Item (const Item& item): x(item.x) {cout << "Item copy constructor called." << endl;}
  Item (Item&& item) : x(std::move(item.x)) {cout << "Item move constructor called." << endl;}
  Item& operator=(const Item& item) { x= item.x; cout << "Item assignment operator called." << endl; return *this;}
  Item& operator=(Item&& item) { x= std::move(item.x); cout << "Item move assignment operator called." << endl; return *this;}

  int x = 0;
};

struct ItemHandler
{
    Item getItem() 
    {
      cout << "getItem called."<< endl; 
      return item;
    }

    Item item{}; 
};

int main()
{
  ItemHandler ih;  
  cout << "trying move assignment" << endl;
  Item&& it = ih.getItem();
}

我原以为是因为ih.getItem()会创建一个副本然后移动分配给它。但这是我得到的输出:

Item constructor called.
trying move assignment
getItem called.
Item copy constructor called.
Item destructor called.
Item destructor called.

1 个答案:

答案 0 :(得分:1)

Item getItem() 
{
    return item;
}

相当于

Item getItem() 
{
    return this->item;
}

this->item是数据成员左值。除非您明确说明,否则编译器不会为您移动它。 Lvalues仅隐式移动for some special cases

解决此问题的正确方法是提供getItem()的参考限定版本:

Item& getItem() &   
{
    return this->item;
}

const Item& getItem() const&
{
    return this->item;
}

Item getItem() &&
{
    return std::move(this->item);
}

然后在主页中使用std::move

int main()
{
    ItemHandler ih;  
    Item it = std::move(ih).getItem();
}