将局部变量命名为右值引用有什么意义吗?
假设代码可以编译,:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
分别在一种情况和另一种情况下调用
operator A&
和operator A&&
可能存在差异(您需要检查规范和 修改/修复了该问题的 DR规范的一部分)。对于这两种情况,绝对不同的是
decltype(a)
。There might be differences with
operator A&
andoperator 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.这有效:
这不起作用:
This works:
This doesn't:
A && 之间的区别a= ...
和A & a= ...
是前者可以绑定到临时变量,而后者则不能。 C++ 标准现在指定引用必须是非易失性 const 或右值引用才能绑定到临时对象(请参阅 8.5.3 引用 [dcl.init.ref]),这样可以延长临时对象的生命周期(请参阅12.2 [类.临时])。编辑:如果您考虑右值引用允许您做什么,它们必须能够绑定到临时变量,否则,您将无法在 C++ 中表达移动语义。
The difference between
A && a= ...
andA & 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++.
(回答我自己的问题只是为了总结我认为我学到的东西。)
总之,
A &a = ...
和A &&a = 之间有什么区别...
?那么在const A &a = ...
和const A &&a = ...
之间呢?如果它们是函数参数的名称,那么它会明显影响函数查找,但我只是在谈论局部变量。区别是:(const
。 const 引用不能用于修改对象。A &a = foo();
无法绑定到临时变量(没有什么新内容),但其他三种形式可以并将生命周期延长到局部变量的生命周期。decltype(a)
会有所不同。operator &
或operator &&
转换可供选择。总而言之,差异比我最初假设的要少。几个月前,我曾以为
会调用
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 = ...
andA &&a = ...
? And betweenconst A &a = ...
andconst 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:const
must be obeyed. A const reference can't be used to modify the object.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.decltype(a)
will be different.operator &
oroperator &&
conversions to choose from.In summary, there are fewer differences than I had originally assumed. Months ago, I had thought that
would call
foo(A&&)
. But insteadfoo(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)