auto && result = foo();
对
decltype(auto) result = foo();
唯一的区别是,当 foo 的返回类型不是引用时,前者的类型是右值引用,而后者的类型不是引用。
声明 | 返回整数 | 返回整数& | 返回整数&& |
---|---|---|---|
汽车 | int | int | int |
自动&& | int&& | int& | int&& |
decltype(自动) | int | int& | int&& |
考虑:
constexpr auto f() -> /* #1 */ {
/* #2 */ r = g();
return r;
}
如果
g
返回纯右值(假设 int
),则:
|
|
结果 |
---|---|---|
|
|
悬挂参考 |
|
|
悬挂参考 |
|
|
悬挂参考 |
|
|
回归
|