返回一个局部变量的std :: move [英] Returning std::move of a local variable

查看:1132
本文介绍了返回一个局部变量的std :: move的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让一个具有移动构造函数的类 A 。考虑这个:

Let there be a class A with a move constructor. Consider this:

A get()
{
    A a;
    return std::move( a );
}

// later in the code
A aa = get();

这里显式调用 std:move 强制 A 的移动构造函数被调用,因此它可能会在调用 get()时禁止返回值优化。因此,据说更好的实现 get()将是这样:

Here the explicit call to std:move forces the move constructor of A to be called thus it might inhibit the return value optimization in while calling get(). Thus it is said the a better implementation of get() would be this:

A get()
{
    A a;
    return a;
}

但是返回值优化不是C ++ 11标准的一部分,因此,如果 WHAT IF ,编译器在调用 get()时会决定不执行返回值优化。在这种情况下, A 的复制构造函数将在 get()中返回时调用?

But the return value optimization is not a part of C++11 standard, so WHAT IF the compiler, by some reason, decides not to perform return value optimization while calling get(). In this case a copy constructor of A will be called while returning in get() right?

所以不是 get()的第一个实现更多pereferible ??

So isn't the first implementation of get() more pereferible??

推荐答案

编译器应使用移动构造函数,但我没有看到标准中的义务:
总是说复制/移动构造函数关于临时对象的部分

A compiler should use a move constructor, but I didn't see an obligation in the standard : It's always said "Copy/move constructor" in the section concerning temporary objects

标准ISO / IEC 14882:2011 C ++:

standard ISO/IEC 14882:2011 C++ :

12.1 / 9

复制构造函数类移动构造函数(12.8)用于移动
类类型对象的内容。

A copy constructor (12.8) is used to copy objects of class type. A move constructor (12.8) is used to move the contents of objects of class type.

12.8 / 32

当满足复制操作的删除标准时,或者满足以下事实,即源
对象是函数参数,要复制的对象由 lvalue 指定,重载解析为
选择该复制的构造函数首先执行,就好像该对象是由一个 rvalue 指定的。如果重载
解析失败,或者如果所选构造函数的第一个参数的类型不是对
的对象类型(可能是cv限定)的 rvalue 引用,再次执行,将对象视为
lvalue 。 [注意:无论是否出现复制偏差
,都必须执行此两阶段重载分辨率。如果不执行elision,它确定要调用的构造函数,并且即使调用被省略,所选择的构造函数
也必须可访问。 - end note]

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If overload resolution fails, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. [ Note: This two-stage overload resolution must be performed regardless of whether copy elision will occur. It determines the constructor to be called if elision is not performed, and the selected constructor must be accessible even if the call is elided. — end note ]

lvalue = T&

rvalue = T&&&< / code>

lvalue = T &
rvalue = T &&

因此,它说,首先,编译器将查找是否找到一个移动构造函数,然后,它将寻找一个移动构造函数。

So, It says that first, the compiler will look if it find a move constructor, then, it will look for a move constructor.

因此,如果你的编译器符合标准,它将调用move构造函数。

Thus, if your compiler is conform to the standard, it will call the move constructor.

我附加有趣的:



12.8 / 31
当满足特定条件时,允许实现省略复制/移动结构一个类
对象,即使该对象的复制/移动构造函数和/或析构函数有副作用。

"
12.8/31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects.
"

...因此,即使这些构造函数/析构函数中存在副作用,也可以跳过

...So even if there is side effects in these constructors/destructors, they can be skipped

这篇关于返回一个局部变量的std :: move的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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