容器中需要显式的移动构造函数吗? [英] Explicit move constructor needed in container?
本文介绍了容器中需要显式的移动构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模板化的容器类:
I have a templated container class:
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);
}
假设数组具有用户定义的移动语义,但Bag没有。在这种情况下,将移动或复制mData吗?
Suppose Array has user-defined move semantics, but Bag does not. Would mData in this case be moved or copied?
推荐答案
它将移动而不是复制。
我建议查看以下图像:
这清楚地表明,只要用户未定义,编译器就会隐式生成一个move构造函数他/她自己的:
This clearly shows that the compiler implicitly generates a move constructor as long as the user doesn't define his/her own :
- 析构函数
- 复制构造函数
- 复制作业
- 移动作业
- destructor
- copy constructor
- copy assignment
- move assignment
由于您的课程没有这些用户定义的构造函数编译器生成的move构造函数将被调用,该构造函数将移动 mData
。
Since your class has none of these user defined constructors the compiler generated move constructor will be called, that constructor will move mData
.
这篇关于容器中需要显式的移动构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文