为什么这里提到纯右值时要用"object"这个词?
Why does it use the term "object" here when mentioning a prvalue?
据我所知,在 c++17 中,prvalue 的 concept/semantic 不再是临时对象 ,因此在许多情况下必须执行复制省略。
然而,今天我看到了 return expression
的描述
If expression is a prvalue, the object returned by the function is initialized directly by that expression. This does not involve a copy or move constructor when the types match
为什么术语对象出现在这里? 值类别中非引用类型的函数return属于prvalue,所以我觉得可能不适合使用术语对象。
据我了解,prvalues现在不再是objects,它们只是值,对吗?
作为补充,here也使用术语"object"。
引用的文本指出存在一个对象,其值设置为函数的 return 值:当 returning [=40= 时使用 C++17 的保证省略]按值,该对象将类似于调用者创建的变量,调用者正在放置的向量中的元素或push_backing,或者在某些动态分配的内存中构造的对象来电者精心策划。您将此与临时混淆,正如您所说,可能不涉及。
系统地研究它,你引用 cppreference 的说法...
return expression;
...那个...
If expression is a prvalue, the object returned by the function is initialized directly by that expression. This does not involve a copy or move constructor when the types match
C++17 标准在 [basic.lval] 中说:
The result of a prvalue
is the value that the expression stores into its context. [ ... ] The result object of a prvalue
is the object initialized by the prvalue
; a prvalue
that is used to compute the value of an operand of an operator or that has type cv void
has no result object. [ Note: Except when the prvalue
is the operand of a decltype-specifier, a prvalue
of class or array type always has a result object. For a discarded prvalue
, a temporary object is materialized; ...
因此,在 cppreference 文本中,标准术语“结果对象”被称为 "the object returned by the function".该语言在说结果对象是 "returned" 而不是 "initialised" 时有点不精确,但总的来说它不会产生误导,并且 - 由于避免了另一个术语 - 对于大多数 cppreference 读者来说可能更容易了解。我没有积极参与该网站,但作为普通用户,我的印象是 cppreference 试图准确解释标准的本质,但尽可能简化语言。
虽然标准未指定底层机制——并且优化的实用性/ABI 决定了不同的实现——但要了解标准要求在功能上发生的事情,可能帮助想象编译器实现代码...
My_Object function() { return { a, b, c }; }
...
... {
My_Object my_object = function(); // caller
}
...通过秘密传递一个memory-for-returned-object地址给函数(很像指向成员函数的this
指针)...
void function(My_Object* p_returned_object) {
new (p_returned_object) My_Object{ a, b, c };
}
因此,调用函数涉及并构造了一个对象,但省略了它的下落 - 即 caller-specified 内存地址。如果函数调用结果未被调用者使用,则至少在概念上构造一个临时对象然后销毁。
我同意你说的。在 cppreference 上有一个讨论页面,您可以在其中提出您的疑虑。更好的表达方式可能是
If expression is a prvalue, the result object of the expression is initialized directly by that expression.
如您所说,纯右值不再返回或传递对象。
据我所知,在 c++17 中,prvalue 的 concept/semantic 不再是临时对象 ,因此在许多情况下必须执行复制省略。
然而,今天我看到了 return expression
的描述If expression is a prvalue, the object returned by the function is initialized directly by that expression. This does not involve a copy or move constructor when the types match
为什么术语对象出现在这里? 值类别中非引用类型的函数return属于prvalue,所以我觉得可能不适合使用术语对象。
据我了解,prvalues现在不再是objects,它们只是值,对吗?
作为补充,here也使用术语"object"。
引用的文本指出存在一个对象,其值设置为函数的 return 值:当 returning [=40= 时使用 C++17 的保证省略]按值,该对象将类似于调用者创建的变量,调用者正在放置的向量中的元素或push_backing,或者在某些动态分配的内存中构造的对象来电者精心策划。您将此与临时混淆,正如您所说,可能不涉及。
系统地研究它,你引用 cppreference 的说法...
return expression;
...那个...
If expression is a prvalue, the object returned by the function is initialized directly by that expression. This does not involve a copy or move constructor when the types match
C++17 标准在 [basic.lval] 中说:
The result of a
prvalue
is the value that the expression stores into its context. [ ... ] The result object of aprvalue
is the object initialized by theprvalue
; aprvalue
that is used to compute the value of an operand of an operator or that has type cvvoid
has no result object. [ Note: Except when theprvalue
is the operand of a decltype-specifier, aprvalue
of class or array type always has a result object. For a discardedprvalue
, a temporary object is materialized; ...
因此,在 cppreference 文本中,标准术语“结果对象”被称为 "the object returned by the function".该语言在说结果对象是 "returned" 而不是 "initialised" 时有点不精确,但总的来说它不会产生误导,并且 - 由于避免了另一个术语 - 对于大多数 cppreference 读者来说可能更容易了解。我没有积极参与该网站,但作为普通用户,我的印象是 cppreference 试图准确解释标准的本质,但尽可能简化语言。
虽然标准未指定底层机制——并且优化的实用性/ABI 决定了不同的实现——但要了解标准要求在功能上发生的事情,可能帮助想象编译器实现代码...
My_Object function() { return { a, b, c }; }
...
... {
My_Object my_object = function(); // caller
}
...通过秘密传递一个memory-for-returned-object地址给函数(很像指向成员函数的this
指针)...
void function(My_Object* p_returned_object) {
new (p_returned_object) My_Object{ a, b, c };
}
因此,调用函数涉及并构造了一个对象,但省略了它的下落 - 即 caller-specified 内存地址。如果函数调用结果未被调用者使用,则至少在概念上构造一个临时对象然后销毁。
我同意你说的。在 cppreference 上有一个讨论页面,您可以在其中提出您的疑虑。更好的表达方式可能是
If expression is a prvalue, the result object of the expression is initialized directly by that expression.
如您所说,纯右值不再返回或传递对象。