将局部变量命名为右值引用有什么意义吗?

发布于 2024-12-21 22:29:09 字数 630 浏览 1 评论 0原文

假设代码可以编译,:

A && a = .....

A &  a = .....

之间有什么区别吗? a 是函数或方法中的局部变量,而不是参数。

通过给右值引用一个名称(a),它实际上是作用域其余部分的左值?即,即使使用前一种形式,在将 a 传递给另一个函数时,您也必须使用 move(a) 来启用窃取?

我知道第二种形式可能存在其他问题,这会阻止编译,例如您不能对临时对象进行(非常量)引用。所以,是的,我很想知道所有差异,但首先我想确认我的预感,即它们对于范围的其余部分是完全等效的。

更新: 作为 @KerrekSB 重申的这个“临时”问题的示例,有时您必须使用简单的引用 const。在这种情况下,我的问题是:

const A && a = .....

const A &  a = .....

Assuming the code compiles, is there any difference between:

A && a = .....

and

A &  a = .....

? a is a local variable in a function or method, not a parameter.

By giving the rvalue-reference a name (a) it is effectively an lvalue for the rest of the scope? i.e. even with the former form, you'd have to use move(a) to enable pilfering when passing a to another function?

I appreciate there might be other problems with the second form, which prevent compilation, for example you can't have a (non-const) reference to a temporary. So, yes, I'm curious to know all the differences, but first I want to confirm my hunch that they are fully equivalent for the remainder of the scope.

Update: as an example of this 'temporary' problem, which @KerrekSB has reiterated, sometimes you must make the plain reference const. In that case, my question is whether there is a difference between:

const A && a = .....

and

const A &  a = .....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软糖 2024-12-28 22:29:09

分别在一种情况和另一种情况下调用 operator A&operator A&& 可能存在差异(您需要检查规范和 修改/修复了该问题的 DR规范的一部分)。

对于这两种情况,绝对不同的是 decltype(a)

There might be differences with operator A& and operator A&& being invoked in the one and other case respectively (You would need to check the spec and the DRs that modified/fixed that part of the spec).

What definitely is different is decltype(a) for both cases.

分開簡單 2024-12-28 22:29:09

这有效:

int foo();

int && a = foo();

这不起作用:

int & b = foo(); // error, cannot bind rvalue to non-const ref

This works:

int foo();

int && a = foo();

This doesn't:

int & b = foo(); // error, cannot bind rvalue to non-const ref
苏佲洛 2024-12-28 22:29:09

A && 之间的区别a= ...A & a= ... 是前者可以绑定到临时变量,而后者则不能。 C++ 标准现在指定引用必须是非易失性 const 或右值引用才能绑定到临时对象(请参阅 8.5.3 引用 [dcl.init.ref]),这样可以延长临时对象的生命周期(请参阅12.2 [类.临时])。

编辑:如果您考虑右值引用允许您做什么,它们必须能够绑定到临时变量,否则,您将无法在 C++ 中表达移动语义。

The difference between A && a= ... and A & a= ... is that the former can bind to a temporary while the latter cannot. The C++ standard now specifies that a reference has to be non-volatile const or an rvalue reference to bind to a temporary (see 8.5.3 References [dcl.init.ref]), which can then extend the lifetime of the temporary (see 12.2 [class.temporary]).

EDIT: If you think about what rvalue references allow you to do, they have to be able to bind to temporaries, otherwise, you would not be able to express move semantics in C++.

葮薆情 2024-12-28 22:29:09

(回答我自己的问题只是为了总结我认为我学到的东西。)

总之,A &a = ...A &&a = 之间有什么区别...?那么在 const A &a = ...const A &&a = ... 之间呢?如果它们是函数参数的名称,那么它会明显影响函数查找,但我只是在谈论局部变量。区别是:(

  1. 明显)必须遵守const。 const 引用不能用于修改对象。
  2. A &a = foo(); 无法绑定到临时变量(没有什么新内容),但其他三种形式可以并将生命周期延长到局部变量的生命周期。
  3. decltype(a) 会有所不同。
  4. (假设不存在 const 问题),初始化可能是相同的,除非有 operator &operator && 转换可供选择。

总而言之,差异比我最初假设的要少。几个月前,我曾以为

A &&a =...;
foo(a);

会调用 foo(A&&)。但需要 foo(move(a))

C++03 程序员可以相当安全地使用 A &&a = 来延长临时变量的生命周期,而不必担心其他意外的差异。

(感谢大家)

(Answering my own question just to summarize what I think I've learned.)

In summary, what's the difference between A &a = ... and A &&a = ...? And between const A &a = ... and const A &&a = ...? If they are names of function parameters, then it affects function lookup clearly, but I'm just talking about local variables. The differences are:

  1. (Obvious) const must be obeyed. A const reference can't be used to modify the object.
  2. A &a = foo(); can't bind to a temporary (nothing new there), but the other three forms can and will extend the lifetime to the of the local variable.
  3. decltype(a) will be different.
  4. (Assuming there was no const problem), the initialization will likely be the same, except if there are operator & or operator && conversions to choose from.

In summary, there are fewer differences than I had originally assumed. Months ago, I had thought that

A &&a =...;
foo(a);

would call foo(A&&). But instead foo(move(a)) is required.

A C++03 programmer can fairly safely use A &&a = to extend the lifetime of temporaries without having to worry about other unexpected differences.

(Thanks to all)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文