c++ - C++中移动构造函数的调用

标签 c++ c++11 move-constructor

我有以下代码:

struct Base
{
    std::vector<int> a;
}

struct Derived : public Base
{
    Derived(Base && rhs):
        Base( std::forward<Base>(rhs))
    {
    }
    //some more fields
}
//...
Base a;
Derived b(std::move(a));

调用 Derived 构造函数是否会导致调用包含在 Base 类中的 std::vector 的移动构造函数?

最佳答案

是的,implicitly-defined move constructor Base 的调用,它将对其数据成员a 执行移动。

For non-union class types (class and struct), the move constructor performs full member-wise move of the object's bases and non-static members, in their initialization order, using direct initialization with an xvalue argument.

LIVE for confirming

关于c++ - C++中移动构造函数的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47049223/

相关文章:

c++ - 模板化 pimpl 转发

c++ - 如何将消息发送到接收期间找到的SCTP关联?

C++ 源文件包含 "Unique Mappings"?

c++ - CPU 松弛指令和 C++11 原语

c++ - 有没有一种可移植的方法来知道 uintptr_t 是否在 stdint.h 中定义?

c++ - 使用指针 vector 实现移动构造函数

c++ - 两步复制省略以在构造函数调用中捕获右值作为实例变量

c++ - 对象超出其范围 C++

c++ - 绑定(bind)到已销毁堆栈变量的 const 引用的生命周期

C++11 move 构造函数有副作用