如何在函数返回期间使用std :: string移动语义? [英] How to use move semantics with std::string during function return?

查看:136
本文介绍了如何在函数返回期间使用std :: string移动语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++ 11值和移动语义混淆

我认为是正确的是

std::string GetLine()
{
std::string str;
std::getline(std::cin, str);
return std::move(str);
}

但在此链接 http://www.cprogramming.com/c++11/rvalue-references-and-move -semantics-in-c ++ 11.html
(检查标头部分从函数返回显式右值引用)

But at this link http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html ( check the header part Returning an explicit rvalue-reference from a function)

是#1 google搜索hit for move语义显示类似于

which is #1 google search hit for move semantics shows a similar function signature as

int&& GetInt()
{
int x = 0;
// code here
return std::move(x);
}

从我在其他地方读到的内容&表示rvalue 引用,因此在这种情况下,它返回对不存在的对象的引用。

From what I read at other places && means rvalue reference so in this case its returning a reference to an object that doesn't exist.

这是什么?

(是的,我知道移动一个int没有真正的有益效果,但问题是是否在第一个函数中使用返回类型的std :: string或std :: string&&&&&&&&&&&)。

(Yes I know moving an int has no real benifit but the question is whether to use the return type of std::string or std::string&& in the first function. And if that is how it should be done for all types.)

int&& GetInt()示例是错误的,并且正在返回对被销毁对象的引用。但是,除非我错过了,您发布的链接实际上不显示任何返回对 变量的引用的代码。

You are absolutely correct that the int&& GetInt() example is wrong, and is returning a reference to an object that is destroyed. However, unless I missed it, the link you posted does not actually show any code returning a reference to a local variable. Instead I see a reference to a global variable being returned, which is okay.

下面是在返回时使用move语义的方法:

Here is how you use move semantics when returning:

std::string func()
{
    std::string rv;
    /* ... */
    return rv;
}

您通常不应使用 std :: move )时返回对象。原因是移动已经被隐含地允许任何时候可能发生RVO,并且使用 std :: move()将抑制RVO。因此,使用 std :: move()永远不会更好,并且通常会比正常返回更糟。

You generally should not use std::move() when returning an object. The reason for this is that moving is already implicitly allowed anytime RVO could occur, and using std::move() will suppress RVO. So using std::move() will never be better and will often be worse than just returning normally.

同样,使用 std :: move()可能比简单地命名要返回的变量更糟糕,因为它抑制返回值优化。返回值优化允许将对象返回给调用者,而不需要在中复制该对象

Again, using std::move() can be worse than simply naming the variable to be returned because it suppresses the return value optimization. The return value optimization allows for an object to be returned to the caller without needing to copy that object



表达式是非易失性自动对象的名称(除函数或catch-clause参数之外的其他
)的返回类型的函数中返回

cv-unsqualified类型作为函数返回类型相同,复制/移动
操作可以通过直接构造自动对象来省略
到函数的返回值

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

- [class.copy] 12.8 / 31

std :: move()可防止返回表达式成为您要返回的对象的名称。相反,表达式更复杂,并且语言不再允许给予特殊处理。

But using std::move() prevents the return expression from being the name of the object you're returning. Instead the expression is more complicated and the language is no longer allowed to give it special handling.

命名对象的原因并不比使用 std :: move()是因为有另一个规则表明一个表达式可以被视为一个右值,而不需要 std :: move >。

The reason just naming the object is not worse than using std::move() is because there's another rule that says an expression can already be treated as an rvalue without needing std::move().


当满足复制操作的删除标准或满足
的标准时,对象是一个函数参数
,并且要复制的对象由一个左值指定,重载
分辨率来选择复制的构造函数首先执行
,就像对象被指定为rvalue。

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.

这篇关于如何在函数返回期间使用std :: string移动语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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