2 An lvalue refers to an object or function. Some rvalue expressions—those of class or cv-qualified class type—also refer to objects.47)
47) Expressions such as invocations of constructors and of functions that return a class type refer to objects, and the implementation can invoke a member function upon such objects, but the expressions are not lvalues. [解决办法] 贴出我的理由: Some rvalue expressions—those of class or cv-qualified class type—also refer to objects. 关于rvalue expressions 标准有一些附注解释: Expressions such as invocations of constructors and of functions that return a class type refer to objects, and the implementation can invoke a member function upon such objects, but the expressions are not lvalues.
再配合: An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to modify its referent under certain circumstances. [Example: a member function called for an object (9.3) can modify the object. ]
虽然上面是说通过成员函数的调用来修改。 [解决办法] fun().i 是右值。 参见 n3290 3.10/1 bullet 4,这一其中红色部分。 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.
下面是一个例子程序证明。
C/C++ code
#include <iostream>struct A{ static int k; int i;};int A::k = 10;A fun( ){ A a; return a; }void f (int& ) { std::cout << "int& " << std::endl; }void f (int&&) { std::cout << "int&&" << std::endl; }int main (){ fun().k = 20; f(fun().i); return 0;} [解决办法]