【发布时间】:2015-03-18 07:57:28
【问题描述】:
在这里,cppreference-lvalue,我发现了
将表达式转换为对函数的右值引用是左值。
我很好奇,于是做了如下实验:
#include <iostream>
using namespace std;
typedef void (&&funr) (int);
typedef void (&funl) (int);
void test(int num){
cout<<num<<endl;//output:20
}
void foo(funr fun){
fun(10);
}
void foo(funl fun){
fun(20);//call this
}
template <typename T> void foo(T&& fun){
cout<<is_same<T,void(&)(int)>::value<<endl;//true, it is lvalue.
cout<<is_same<T,void(int)>::value<<endl;//false, it isn't rvalue.
}
int main()
{
foo(static_cast<void(&&)(int)>(test));
return 0;
}
事实如此。
为什么将表达式转换为右值 对函数的引用是左值? 是因为函数类型不需要移动语义还是其他什么? 或者我把这个词理解错了。
将表达式转换为对函数的右值引用是左值
【问题讨论】:
-
我认为技术上的答案是因为标准在 [conv] / 6、[expr.static.cast] / 1 等中这么说。
-
一个很好的理由是here(在 cmets 中有额外的说明)