[英]Are move constructors/assignment operators generated for derived classes
If class X is derived from class Y and class Y has any of: 如果X类是从Y类派生的,并且Y类具有以下任一项:
Will a move constructor and move assignment operator be implicitly defaulted for class X providing it declares none of the above? 如果X类没有声明上述任何一项,是否会将移动构造函数和移动赋值运算符隐式默认为类X?
eg 例如
struct Y
{
virtual ~Y() {}
// .... stuff
};
struct X : public Y
{
// ... stuff but no destructor,
// no copy/move assignment operator
// no copy/move constructor
// will X have a default move constructor / assignment operator?
};
I am currently using gcc, but I am mainly interested in what the correct behaviour should be (as opposed to whether or not a particular compiler is standards compliant).. 我目前正在使用gcc,但主要是对正确的行为(相对于特定的编译器是否符合标准)感兴趣。
As far as the derived class is concerned, the fact that an attribute or a base class has user-defined or implicit special functions does not matter. 就派生类而言,属性或基类具有用户定义的或隐式的特殊功能这一事实无关紧要。 All that matter is their presence.
重要的是他们的存在。
Compliant C++11 compilers should automatically provide the move constructors and assignment operators for the struct and class, if possible (which is clearly defined in the Standard), even though only classes with dynamically allocated buffers will really benefit from it (moving an int
is just copying it). 兼容的C ++ 11编译器应自动为struct和类(在标准中已明确定义)提供struct和class的move构造函数和赋值运算符,即使只有具有动态分配缓冲区的类才能真正从中受益(移动
int
只是复制它)。
Therefore, if your class embeds a std::string
or a std::unique_ptr
(for example), then its move constructor will call the embedded string
or unique_ptr
move constructor and it will be efficient... for free. 因此,如果您的类嵌入了
std::string
或std::unique_ptr
(例如),则其move构造函数将调用嵌入的string
或unique_ptr
move构造函数,这将是有效的...免费的。
Simply changing the compilation mode should thus slightly improve performance. 只需更改编译模式即可稍微提高性能。
Yes, they are, in §12.8.9: 是的,在§12.8.9中:
If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if 如果类X的定义未明确声明移动构造函数,则仅当且仅当将隐式声明为默认构造函数。
and §12.8.10 和§12.8.10
The implicitly-declared move constructor for class X will have the form 类X隐式声明的move构造函数将具有以下形式
X::X(X&&)
Similarly, for move assignment operators in 12.8.20: 同样,对于12.8.20中的移动分配运算符:
If the definition of a class X does not explicitly declare a move assignment operator, one will be implicitly declared as defaulted if and only if 如果类X的定义未明确声明移动赋值运算符,则仅当且仅当将隐式声明为默认值
Base classes don't come into it directly. 基类不直接进入它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.