返回const引用和右值引用之间的区别 [英] Difference between returning a const reference and rvalue reference

查看:232
本文介绍了返回const引用和右值引用之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没记错,我认为const引用和右值引用都可以绑定到右值。返回前者的函数和返回后者的函数有什么实际区别?

If I'm not wrong, I think that both a const reference and a rvalue reference can bind to a rvalue. Is there any practical difference between a function that returns the former and a function that returns the latter?

EDIT。我不能修改前者,但是为什么我会对修改右值感兴趣?

EDIT. I cannot modify the former, but why would I be interested in modifying a rvalue? Does it make sense?

推荐答案

一个 const 左值引用可以绑定到任何东西。右值引用只能绑定到非 const 右值。

A const lvalue reference can bind to anything. An rvalue reference can only bind to non-const rvalues.

            non-const lvalue   const lvalue   non-const rvalue   const rvalue
const T&    yes                yes            yes                yes
T&&         no                 no             yes                no

如您所见,它们是非常不同的。

As you can see, they are very different.

此外,如果函数调用返回左值引用,则该表达式为左值,但如果函数调用返回对对象的右值引用,则该表达式为xvalue

In addition, if a function call returns an lvalue reference, that expression is an lvalue, but if a function call returns an rvalue reference to object, that expression is an xvalue.


如果结果类型是左值引用类型或右值引用到函数类型,则函数调用为左值,如果结果类型为左值引用,则为xvalue。结果类型是对对象类型的右值引用,否则为prvalue。

A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise.

关于何时要修改右值-很好这正是移动语义的全部含义。考虑以下函数调用:

As for when you would want to modify an rvalue - well this is precisely what move semantics are all about. Consider the following function call:

void func(std::string);

func(std::string("Hello"));

表达式 std :: string( Hello)是一个创建临时对象的右值。在使用此右值初始化 std :: string 参数时,它将选择采用右值引用的构造函数- move构造子。然后,此构造函数从右值中窃取东西,通常比进行完整复制要快得多。我们可以从中窃取,因为我们知道是临时的。

The expression std::string("Hello") is an rvalue that creates a temporary object. When initializing the std::string parameter with this rvalue, it will choose the constructor that takes an rvalue reference - the move constructor. This constructor then steals things from the rvalue, which is typically much faster than doing a full copy. We can steal from it because we know it's temporary.

至于何时应返回 const 左值引用或右值引用:

As for when you should return const lvalue references or rvalue references:


  • 返回 const 当您想授予读取内部对象(也许是类的成员)的权限但不允许人们对其进行修改时,最常使用左值引用。

  • Returning a const lvalue reference is most commonly used when you want to give access to read an "internal" object (perhaps a member of a class), but not allow people to modify it.

当您想允许调用代码从内部对象(也许是类的成员)移动时,最常使用返回右值引用(根本不常见)。因此,它们不是从临时返回的对象移动(就像按值返回时那样),而是从内部对象移动。

Returning an rvalue reference is most commonly used (not common at all) when you want to allow calling code to move from an "internal" object (perhaps a member of a class). So instead of moving from a temporary returned object (as they would when returning by value), they literally move from the internal object.

- const 左值引用,但随后他们必须明确地它。

This could also be achieved with a non-const lvalue reference, but then they would have to explicitly std::move it.

因此不太可能需要返回右值引用。

So it's not very likely that you'll need to return an rvalue reference.

不是 std :: forward 有返回类型类似于 T&& 的返回类型。但是,这具有欺骗性,因为根据 T 的类型,它可能是右值引用,也可能不是。参见通用引用

Not that std::forward has a return type that looks like T&&. However, this is deceptive, because it may or may not be an rvalue reference depending on the type of T. See universal references.

这篇关于返回const引用和右值引用之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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