2015-05-29 28 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

非常好,謝謝。 –