简体   繁体   中英

rvalue reference to a function type

As known, the function call which return type is an rvlaue to a function is an lvalue.

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
}

What's wrong with that diagnostic message?

Emphasis mine, [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.

So you may have an lvalue referring to a function but it is not a modifiable lvalue, and cannot be used to modify the function.

The diagnostic message... leaves something to be desired. Clang 3.6 says,

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

which is clearer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM