c++ - 为 shared_ptr<MyClass> 的 vector move 构造函数

标签 c++ boost c++11 move shared-ptr

我知道如果你想传递一个 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/

相关文章:

c++ - 在父类(super class)构造函数中引用时调用哪个公共(public)函数?

c++ - 使用 boost iostreams 过滤器(关闭且不可复制)

c++ - Boost.Program_Options:为什么 options_description_easy_init::operator() 没有 std::string 的重载?

C++11 通用初始化导致意外初始化?

c++ - 正则表达式替换方法名称

c++ - 在调用 QGraphicsItem::paint 之前

python - 在阻塞的 boost c++ 方法中,如何在 Python 中捕获中断信号?

c++ - 如果在 C++ 中的基类中添加虚函数或非虚函数,是否必须重新编译整个类层次结构?

c++ - 与 move 构造函数配对时发生意外的析构函数调用

c++ - 在 C++ 中使用 <elem> 对函数