为什么const函数返回左值而不是右值?

Alo*_*lok 1 c++ reference rvalue lvalue c++11

ff()函数返回一个右值但是当我更改函数的返回值时const,它是否返回左值?为什么下面的输出改变其输出"lvalue reference""rvalue reference"当我改变 test ff() { }const test ff() { }

#include <iostream>
using namespace std;
class test { };
void fun( const test& a)
{
    cout << "lvalue reference"<<endl;
}
void fun(  test&& a)
{
    cout << "rvalue reference"<<endl;
}
const test ff() { } // <<---return value is const now
int main()
{

  fun(ff());
}     
Run Code Online (Sandbox Code Playgroud)

输出:

lvalue reference
Run Code Online (Sandbox Code Playgroud)

Nat*_*ica 9

void fun( test&& a)是一个引用非const rvalue的函数. ff返回一个const testconst rvalue.你不能绑定一个参照非const右值到一个const右值,因为这将违反常量,正确性.这就是它改为绑定的原因void fun( const test& a),它引用了一个const test


请注意,按值返回时,有没有好处返回const thingthing.添加const到返回类型事项的唯一时间是通过引用返回时.如果您有作为标记的成员函数const或引用返回恒定的数据成员,那么你必须使用const保存常量,正确性.