移动派生类的构造函数 [英] Move constructor for derived class

查看:73
本文介绍了移动派生类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个课程:

template<typename T>
class base{
    T t;
public:
    base(base &&b): t(std::move(b.t)){}
};

template<typename T, typename T2>
class derived : protected base<T>{
    T2 t2;
public:
    derived(derived &&d): base<T>(std::move(d)), t2(std::move(d.t2)){}
};

我将整个d对象移到derived move-constructor中以初始化base部分,并且d变为无效,但是我仍然需要它来将其用于t2初始化

I move entire d object in the derived move-constructor to initialize base part and d becomes invalid but I still need it to use it's part for t2 initialization

有可能做这样的事情吗?

Is it possible to do such a thing?

推荐答案

我会说您的构造是正确的,除了一些语法错误,您需要在初始化列表中限定base<T>:

I would say that your construct is correct except for a little syntax error, you need to qualify base<T> in the initializer list:

derived(derived &&d): base<T>(std::move(d)), t2(std::move(d.t2)){}

首先,初始化的顺序与初始化列表的顺序无关. n4296草案在 12.6.2中声明初始化基础和成员[class.base.init]§13

First, the order of initialization is independant of the order of the initializer list. Draft n4296 says in 12.6.2 Initializing bases and members [class.base.init] § 13

在非委托构造函数中,初始化按以下顺序进行:
(13.1)—首先,并且仅对于最派生类(1.8)的构造函数,虚拟基类在 它们在基类的有向无环图的深度优先从左到右遍历时出现的顺序, 其中从左到右"是基类在派生类base-specifier-list中的出现顺序.
(13.2)—然后,直接基类按照它们出现在base-specifier-list中的声明顺序进行初始化 (与mem-initializers的顺序无关).
(13.3)—然后,按照在类定义中声明的顺序初始化非静态数据成员 (同样,无论mem-initializers的顺序如何).
(13.4)—最后,执行构造函数主体的复合语句.

In a non-delegating constructor, initialization proceeds in the following order:
(13.1) — First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where "left-to-right" is the order of appearance of the base classes in the derived class base-specifier-list.
(13.2) — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
(13.3) — Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
(13.4) — Finally, the compound-statement of the constructor body is executed.

[注意:声明顺序是强制性的,以确保在子对象中销毁基础和成员子对象. 初始化的相反顺序. —尾注]

[ Note: The declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. —end note ]

我们也有§7或同一章说:

We have also §7 or same chapter that says:

每个mem-initializer执行的初始化构成一个完整表达式.任何 mem初始化程序中的表达式将作为执行初始化的全表达式的一部分进行评估.

The initialization performed by each mem-initializer constitutes a full-expression. Any expression in a mem-initializer is evaluated as part of the full-expression that performs the initialization.

我的理解是,标准表示在derived类的移动ctor中,事情按以下顺序发生:

My understanding is that standard says that in the move ctor for the derived class, things happens in that order:

  • 为基类移动ctor称为
    • 依次调用T的move ctor有效地构造目标的t成员,并最终将源的t成员清零
    • move ctor for base class is called
      • in turn it calls move ctor for T effectively constructing t member of target and eventually zeroing t member of source

      这篇关于移动派生类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆