2015-05-29 60 views
0

众所周知,函数调用哪个返回类型是函数的函数是一个左值。函数类型的右值引用

A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise.

#include <iostream> 

int a(){ return 1; } 

int foo(){ return 1; } 

int (&&bar())(){ return a; } 

int main() 
{ 
    bar() = foo; //error: cannot convert 'int()' to 'int()' in assignment 
} 

什么是错的诊断消息?

回答

6

重点矿山,[expr.ass]/1:

The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand...

[basic.lval]/6:

Functions cannot be modified, but pointers to functions can be modifiable.

所以,你可能有一个左值参照的功能,但它是而不是可修改的左值,并且不能用于修改该函数。

诊断消息...留下了一些需要的东西。铿锵3.6说,

error: non-object type 'int()' is not assignable

这是更清晰。

+0

非常好,谢谢。 –