【发布时间】:2021-06-13 07:10:09
【问题描述】:
我有以下代码sn-p:
#include <iostream>
using namespace std;
class X {
int i;
public:
X(int ii = 0);
void modify();
};
X::X(int ii) { i = ii; }
void X::modify() { i++; }
X f5() { return X(); }
const X f6() { return X(); }
void f7(X& x) {
x.modify();
}
int f()
{
return 18;
}
int main() {
f5() = X(1); /// Why does this work??? Isn't f5() an rvalue ??? (*)
f5().modify();
/// f7(f5()); /// cannot bind non-const lvalue reference of type 'X&' to an rvalue of type X; not contradictory with (*)?
//! f6() = X(1);
//! f6().modify();
//! f7(f6());
//! f() = 12; this also doesn't work
return 0;
}
f5() = X() 如何工作? f5() 不是右值吗?那为什么f() = 12 不起作用?有什么不同 ?另外,f7(f5()) 生成的错误并不是说f5() 是右值?我错过了什么?
【问题讨论】: