Rvalue引用被视为Lvalue? [英] Rvalue Reference is Treated as an Lvalue?

查看:313
本文介绍了Rvalue引用被视为Lvalue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了此答案: http://stackoverflow.com/a/28459180/2642059 其中包含以下代码:

  void foo(string&& bar){
string * temp =& bar;

cout<< * temp< @:<<温度< endl;
}

bar rvalue或一个左值?



我问,因为我显然不能获取右值的地址,但我可以像这里一样获取右值引用的地址。 p>

如果你可以对右值引用执行任何操作,你可以在左值引用上区分两个与&& / code>一个右值或一个左值?


无论名字是一个小值(1)。因此 bar 是一个左值。其类型是右值引用 string ,但它是该类型的左值。



如果您想将其视为右值,您需要将 std :: move()应用到它。







如果您可以对右值引用执行任何操作,您可以在左值引用上区分两者与&&而不只是一个&?


这取决于你对执行操作的定义。一个左值引用和一个(命名)右值引用在表达式中如何使用它们是非常相同的,但是它们在可以绑定到它们方面有很多不同。 Lvalues可以绑定到Lvalue引用,Rvalue可以绑定到Rvalue引用(任何东西都可以绑定到一个Ivalue引用 const )。也就是说,您不能将右值绑定到左值引用,反之亦然。



让我们谈谈一个rvalue引用类型的函数参数(例如 bar )。重要的不是 bar 是什么,而是你知道 bar 所指的值。因为 bar 是一个右值引用,你知道无论与它绑定的是一个右值。这意味着它的约束将被销毁,表达式结束,你可以安全地将其视为一个右值(通过窃取其资源等)。



如果你不是直接执行这个操作 bar ,但只想传递 bar ,您有两个选择:您完成了 bar ,然后你应该告诉接收它的下一个它绑定到右值— do std :: move(bar)。或者你需要使用 bar 做一些更多的事情,所以你不想让任何人在你下面窃取它的资源,所以只是把它当作一个左值— code> bar 。



总结:区别在于您可以使用参考






(1)一个很好的经验法则,除了少数例外:枚举器有名字,但是是右值。类,命名空间和类模板具有名称,但不是值。


I posted this answer: http://stackoverflow.com/a/28459180/2642059 Which contains the following code:

void foo(string&& bar){
    string* temp = &bar;

    cout << *temp << " @:" << temp << endl;
}

Is bar an rvalue or an lvalue?

I ask because I obviously cannot take the address of an rvalue, yet I can take the address of an rvalue reference as is done here.

If you can perform any operation on an rvalue reference that you can on an lvalue reference what is the point in differentiating between the two with the "&&" instead of just an "&"?

解决方案

Is bar an rvalue or an lvalue?

The question answers itself. Whatever has a name is an lvalue(1). So bar is an lvalue. Its type is "rvalue reference to string", but it's an lvalue of that type.

If you want to treat it as an rvalue, you need to apply std::move() to it.


If you can perform any operation on an rvalue reference that you can on an lvalue reference what is the point in differentiating between the two with the "&&" instead of just an "&"?

This depends on your definition of "perform an operation." An lvalue reference and a (named) rvalue reference are pretty much identical in how you can use them in expressions, but they differ a lot in what can bind to them. Lvalues can bind to lvalue references, rvalues can bind to rvalue references (and anything can bind to an lvalue reference to const). That is, you cannot bind an rvalue to an lvalue reference, or vice versa.

Let's talk about a function parameter of rvalue reference type (such as your bar). The important point is not what bar is, but what you know about the value to which bar refers. Since bar is an rvalue reference, you know for certain that whatever bound to it was an rvalue. Which means it's bound to be destroyed when the full expression ends, and you can safely treat it as an rvalue (by stealing its resources etc.).

If you're not the one doing this directly to bar, but just want to pass bar on, you have two options: either you're done with bar, and then you should tell the next one who receives it that it's bound to an rvalue—do std::move(bar). Or you'll need to do some more things with bar, and so you do not want anyone inbetween stealing its resources from under you, so just treat it as an lvalue—bar.

To summarise it: The difference is not in what you can do with the reference once you have it. The difference is what can bind to the reference.


(1) A good rule of thumb, with minor exceptions: enumerators have names, but are rvalues. Classes, namespaces and class templates have names, but aren't values.

这篇关于Rvalue引用被视为Lvalue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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