The Structured binding Case2 in cppreference is a bit hard to understand. Basically, I want a clarification of these situations
int x = 1;
double y = 2.0;
auto [a, b] = std::forward_as_tuple(x, y); //a, b are both reference, why?
auto&& [c, d] = std::forward_as_tuple(x, y); //What's the difference of this and above?
auto&& [e, f] = std::tuple{x, y}; //why are NOT e, f rvalue references? Resharper shows them as value type not reference type
And if there is some function return tuple of reference, how can I make a copy using structured binding?
std::tuple<int&, double&> f;
auto [x, y] = f(); //But I want a copy from the reference, how?
std::forward_as_tuple(x, y)
gives you atuple<int&, double&>
. The types of the bindings into that areint&
anddouble&
(in the same way that the types of the bindings intotuple<int, double>
areint
anddouble
). Basically:behaves as if:
So
a
is an rvalue reference toint&
andc
is an rvalue reference todouble&
, soint&
anddouble&
respectively. This particular formulation (I'm specifically calling it a reference to reference, rather than calling it justint&
) is necessary becausedecltype(name)
wherename
is a structured binding gives you the referenced type, which is whydecltype(a)
would give youint&
.The above also shows the difference between the
[a, b]
and the[c, d]
case: theauto
vsauto&&
declaration applies to the unnamed object that we're destructuring. It does not affect the bindings themselves†.This case:
Does not give references because it unpacks to:
So
e
is an rvalue reference toint
, which meansdecltype(e)
isint
, notint&
.You can't make a copy using structured bindings. Structured bindings is solely about destructuring an object, it is not at all about changing anything. If you want to make a copy, you have to do that manually:
†In the above case, because the underlying object being destructured is an lvalue reference (
auto&
), this technically makes the bindings themselves lvalue references to whatever instead of rvalue references to whatever -- although I'm not sure if this is actually a meaningful distinction.