容器中需要显式移动构造函数吗?

时间:2017-06-15 17:11:03

标签: c++ c++11

我有一个模板化的容器类:

 template<class Stuff>
 class Bag{
     private:
        std::vector<Stuff> mData;
 };

我想做

  void InPlace(Bag<Array>& Left){
      Bag<Array> temp;
      Transform(Left, temp); //fills temp with desirable output
      Left = std::move(temp);
  }

假设Array具有用户定义的移动语义,但Bag没有。在这种情况下,mData会被移动还是复制?

1 个答案:

答案 0 :(得分:8)

它会被移动,而不是复制。

我建议查看以下图片:

enter image description here

这清楚地表明只要用户没有定义他/她自己,编译器就会隐式生成移动构造函数:

  • 复制构造函数
  • 复制作业
  • 移动作业

由于您的类没有这些用户定义的构造函数,因此将调用编译器生成的移动构造函数,该构造函数将移动mData

相关问题