为什么在下面的代码(包括gcc 7.2和clang 4.0)Base中继承(class B)的情况下,移动构造函数是强制性的?我希望在C++ 17中不需要保证复制省略,就像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
Run Code Online (Sandbox Code Playgroud)
考虑以下代码:
#include <iostream>
class Data{
public:
Data() = default;
Data(Data const&) = delete;
Data(int) {
}
};
int main(){
int a = 0;
const std::string& rs = "abc"; // rs refers to temporary copy-initialized from char array
Data const& d_rf = a; // #2 but here can be complied
// accroding to the standard, the reference in #2 is bound to a temporary object, the temporary is copy-initialized from the expression
}
Run Code Online (Sandbox Code Playgroud)
如果T1或T2是一个类类型和T1不参考相关到T2,用户定义的转换正在使用的规则考虑复制初始化型“CV1 T1”的对象的由用户定义的转换([dcl.init ], [over.match.copy], [over.match.conv]); …
c++ language-lawyer implicit-conversion c++11 reference-binding