Reputation: 353
auto && result = foo();
vs
decltype(auto) result = foo();
the only difference is that when the return type of foo is not reference, the type of former is rvalue reference but the type of latter is not reference.
declare | return int | return int& | return int&& |
---|---|---|---|
auto | int | int | int |
auto&& | int&& | int& | int&& |
decltype(auto) | int | int& | int&& |
Upvotes: 2
Views: 165
Reputation: 303007
Consider:
constexpr auto f() -> /* #1 */ {
/* #2 */ r = g();
return r;
}
if g
returns a prvalue (let's say int
), then:
#1 |
#2 |
result |
---|---|---|
auto&& |
auto&& |
dangling reference |
auto&& |
decltype(auto) |
dangling reference |
decltype(auto) |
auto&& |
dangling reference |
decltype(auto) |
decltype(auto) |
returns int |
Upvotes: 4