FCD 中的右值和临时对象
我花了相当长的时间才理解右值和临时对象之间的区别。但现在委员会最终草案在第 75 页指出:
右值 [...]是x值、临时对象或其子对象,或者不与一个对象。
我简直不敢相信自己的眼睛。这一定是一个错误,对吧?
为了澄清,我是这样理解这些术语的:
#include <string>
void foo(std::string&& str)
{
std::cout << str << std::endl;
}
int main()
{
foo(std::string("hello"));
}
在这个程序中,有两个表达式表示同一个临时对象:纯右值std::string( “hello”)
和左值 str
。表达式不是对象,但它们的求值可能会产生一个对象。具体来说,纯右值的求值会产生一个临时对象,但纯右值不是临时对象。有人同意我的观点还是我疯了? :)
It took me quite some time to understand the difference between an rvalue and a temporary object. But now the final committee draft states on page 75:
An rvalue [...] is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object.
I can't believe my eyes. This must be an error, right?
To clarify, here is how I understand the terms:
#include <string>
void foo(std::string&& str)
{
std::cout << str << std::endl;
}
int main()
{
foo(std::string("hello"));
}
In this program, there are two expressions that denote the same temporary object: the prvalue std::string("hello")
and the lvalue str
. Expressions are not objects, but their evaluation might yield one. Specifically, the evaluation of a prvalue yields a temporary object, but a prvalue IS NOT a temporary object. Does anyone agree with me or have I gone insane? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我同意你的观点。在我看来,这个问题应该得到解决,而且我深深尊敬的几个人也对此提出了完全相同的问题。
Yes, i agree with you. This should be fixed in my opinion, and several people i deeply pay respect to have risen the exact same question about this.
这并不像听起来那么复杂。我指的是现已最终确定的标准 ISO/IEC 14882-2011。第78页说:
上面的粗体是我加上去的。该标准进一步指出:
因此,只有当您使用“涉及右值引用的某些类型的表达式”时,您才会获得 xvalue。否则你的临时对象就只是临时对象。
This isn't as complicated as it sounds. I am referring to the now finalized standard ISO/IEC 14882-2011. Page 78 says:
The bold above has been added by me. The standard further says:
So you only get an xvalue when you are playing around with 'certain kinds of expressions involving rvalue references'. Otherwise your temporary objects are just that - temporary objects.