RVO为什么不应用于基类子对象初始化? [英] Why isn't RVO applied to base class subobject initialization?
本文介绍了RVO为什么不应用于基类子对象初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下代码中,为什么在继承(类B
)的情况下必须强制 Base
的move构造函数(都在gcc 7.2和clang 4.0中)?我希望C ++ 17中不需要保证复制保留就不需要它,例如在合成的情况下( class A
)。
Why is the move constructor for Base
mandatory in case of inheritance (class B
) in the following code (both in gcc 7.2 and clang 4.0)? I would expect it not to be required with guaranteed copy elision in C++17, as in case of composition (class A
).
struct Base {
Base(Base&&) = delete;
Base& operator=(Base&&) = delete;
Base()
{
}
};
Base make_base()
{
return Base{};
}
struct A {
A() : b(make_base()) {} // <<<--- compiles fine
Base b;
};
#ifdef FAIL
struct B : public Base {
B() : Base(make_base()) {} // <<<--- "Base(Base&&) is deleted"
};
#endif
推荐答案
这是标准措辞中的一个缺陷。初始化基类子对象时,不能保证复制省略,因为基类的布局可能不同于相应的完整对象类型。
This is a defect in the standard wording. Copy elision cannot be guaranteed when initializing a base class subobject, because base classes can have different layout than the corresponding complete object type.
这篇关于RVO为什么不应用于基类子对象初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文