FCD 中的右值和临时对象

发布于 2024-09-05 03:40:07 字数 597 浏览 5 评论 0原文

我花了相当长的时间才理解右值和临时对象之间的区别。但现在委员会最终草案在第 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 技术交流群。

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

发布评论

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

评论(2

箹锭⒈辈孓 2024-09-12 03:40:07

是的,我同意你的观点。在我看来,这个问题应该得到解决,而且我深深尊敬的几个人也对此提出了完全相同的问题。

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.

情话墙 2024-09-12 03:40:07

这并不像听起来那么复杂。我指的是现已最终确定的标准 ISO/IEC 14882-2011。第78页说:

xvalue(“eXpiring”值)也指一个对象,通常靠近
其生命周期结束(以便其资源可以被移动,例如
例子)。 xvalue 是某些类型表达式的结果
涉及右值引用
(8.3.2)。

上面的粗体是我加上去的。该标准进一步指出:

右值(历史上如此称呼,因为右值可能出现在
赋值表达式的右侧)是 xvalue,a
临时对象(12.2)或其子对象,或不是的值
与对象关联。

因此,只有当您使用“涉及右值引用的某些类型的表达式”时,您才会获得 xvalue。否则你的临时对象就只是临时对象。

This isn't as complicated as it sounds. I am referring to the now finalized standard ISO/IEC 14882-2011. Page 78 says:

An xvalue (an “eXpiring” value) also refers to an object, usually near
the end of its lifetime (so that its resources may be moved, for
example). An xvalue is the result of certain kinds of expressions
involving rvalue references
(8.3.2).

The bold above has been added by me. The standard further says:

An rvalue (so called, historically, because rvalues could appear on
the right-hand side of an assignment expression) is an xvalue, a
temporary object (12.2) or subobject thereof, or a value that is not
associated with an object.

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.

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