gpt4 book ai didi

c++ - 默认情况下函数返回值是常量(右值)吗?

转载 作者:太空狗 更新时间:2023-10-29 23:46:52 26 4
gpt4 key购买 nike

我正在学习右值引用,教程告诉我:

X foo();
X x;
x = foo();

Rather obviously, it would be ok, and much more efficient, to swap resource pointers (handles) between x and the temporary, and then let the temporary's destructor destruct x's original resource.

In other words, in the special case where the right hand side of the assignment is an rvalue, we want the copy assignment operator to act like this.

那么,这是否意味着默认情况下函数的返回值总是常量,因此是右值?如果是:它们是否始终不变,还是也有异常(exception)?

最佳答案

Rvalue-ness 和 constant-ness 不是同义词,而是有点正交。具有以下定义:

struct X {};
const X x;
const X f();
int X();

我们可以对以下表达式进行分类:

x;       // constant lvalue
f(); // constant rvalue
g(); // non-constant rvalue

关于您的特定问题:不,并非所有右值表达式都是常量。

关于c++ - 默认情况下函数返回值是常量(右值)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8467914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com