对函数类型的右值引用
rvalue reference to a function type
众所周知,return类型为函数右值的函数调用是左值。
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
}
该诊断消息有什么问题?
强调我的,[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.
所以你可能有一个引用函数的左值,但它不是可修改的左值,不能用于修改函数。
诊断消息...还有待改进。 Clang 3.6 说,
error: non-object type 'int ()' is not assignable
哪个更清楚
众所周知,return类型为函数右值的函数调用是左值。
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
}
该诊断消息有什么问题?
强调我的,[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.
所以你可能有一个引用函数的左值,但它不是可修改的左值,不能用于修改函数。
诊断消息...还有待改进。 Clang 3.6 说,
error: non-object type 'int ()' is not assignable
哪个更清楚