typedef void(&&RF)(void* p);
RF rf()
{
return f;
}
int ay[10] = { 0 };
typedef int(&&RA)[10];
RA ra()
{
return ay; // error
}
cout << is_lvalue_reference<decltype(rf())>::value << endl; // 1
Run Code Online (Sandbox Code Playgroud)
C++参考文献说"对函数的rvalue引用被视为左值,无论是否命名".
但我无法理解这是什么考虑因素?我想也许函数的名称总是左值.所以它必须保持左值的属性,并确保将函数名称传递给可以调用的任何地方,比如rf()(NULL).然后数组名称在我脑海中被禁止.我认为它也总是一个左值,所以我编写了上面的代码来测试它并得到一个错误.
谁能指出所有这一切背后的真正原因?
I have the situation where one function calls one of several possible functions. This seems like a good place to pass a function as a parameter. In this Quoara answer by Zubkov there are three ways to do this.
int g(int x(int)) { return x(1); }
int g(int (*x)(int)) { return x(1); }
int g(int (&x)(int)) { return x(1); }
...
int f(int n) { return n*2; }
g(f); // all three g's above work the same
Run Code Online (Sandbox Code Playgroud)
When should which …