简体   繁体   English

C ++ 11移动构造函数和赋值运算符

[英]C++11 move constructor and assignment operator

A relatively simple question on the implicit usage of move semantics, 关于移动语义的隐式用法的一个相对简单的问题,

When we have 当我们有

A func();

The following code: 以下代码:

A a;

a = func();

will call A's default constructor then A's copy constructor to create/return the temporary and then the copy assignment operator to assign it to object a. 将调用A的默认构造函数,然后调用A的复制构造函数来创建/返回临时,然后复制赋值运算符将其分配给对象a。

In case a move constructor and a move assignment have been defined for A, what will be actually called in the last statement for the temporary/rvalue to be created? 如果已经为A定义了移动构造函数和移动赋值,那么在最后一个语句中实际调用什么来创建临时/ rvalue? Will it be the copy constructor followed by move assignment? 是复制构造函数后跟移动赋值吗?

Creating the temporary is done with the move-constructor, if there is one and the return value can be treated as an rvalue , otherwise the copy-constructor. 使用move-constructor创建临时文件,如果有,则返回值可以视为右值 ,否则为copy-constructor。 This might be elided, if the function is suitable for return-value optimisation. 如果该函数适用于返回值优化,则可能会省略此操作。

Assigning to a is done with the move-assignment operator if there is one, otherwise, the copy-assignment operator. 如果有移动赋值运算符,则使用移动赋值运算符分配给a ,否则为复制赋值运算符。 This is because the temporary is an rvalue . 这是因为临时是一个右值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM