我有以下代码
decltype(auto) foo(){
int a = 10;
return a;
}
我本来希望 foo 的类型是
int & foo()
因为a是左值,而 decltype(a) 是参考。
如果我做了一个非常相似的事情,但用一个完美的转发 lambda 混淆它,我会得到不同的结果。
// perfect forwarding lambda
auto identity = [](auto&& i) -> decltype(auto) { return std::forward<decltype(i)>(i); };
decltype(auto) foo(){
int a = 10;
return identity(a);
}
foo 的返回类型变成int&
任何人都可以告诉我这里发生的事情的确切规则吗?