我知道如果你想传递一个 MyClass 对象的 vector 并且它是一个临时变量,如果有一个为 MyClass 定义的 move 构造函数那么这将被调用,但是如果你传递一个 boost::shared_ptr<MyClass>
的 vector 会发生什么?或 std::shared_ptr<MyClass>
? shared_ptr 是否有一个 move 构造函数,然后调用 MyClass 的 move 构造函数?
最佳答案
if there is a move constructor defined for MyClass then this will be called
通常不会。 move 一个 vector 通常是在我转移托管数组的所有权时完成的,将移出的 vector 留空。对象本身没有被触及。 (我认为如果这两个 vector 具有不兼容的分配器可能会出现异常,但这超出了我需要处理的任何事情,所以我不确定那里的细节)。
Does the shared_ptr have a move constructor which then call's MyClass's move constructor?
没有。同样,它有一个 move 构造函数,该构造函数将 MyClass
对象的所有权转移给新指针,将旧指针留空。对象本身未被触及。
关于c++ - 为 shared_ptr<MyClass> 的 vector move 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20246273/