structural_binding
- Reading notes from
- Code playground
Structural binding
Quick fact
- "structured binding" feature can be seen a syntax-sugar. This means that we can produce a code equivalent to what a structured binding declaration would do.
auto [x, y] = foo();
- Under the hood:
- I am declaring a new anonymous variable that encompass
[x, y]. - This anonymous variable will have its type deduced and be a copy of what
fooreturns since we haveauto.
- I am declaring a new anonymous variable that encompass
// e.g. under the hood
auto a_secret_variable = foo();
-
What this implies is
- the
autokeyword is applied to the anonymous variable - the
autokeyword is NOT applied to the identifierxandy
- the
-
Anonymous variable itself is not visible to anyone except the compiler, it is not possible to actually check its type.
-
Now let's assume that
foois returning a type by value -T foo()- and therefore produce a temporary variable. -
This means that we can only use
auto,const auto&orauto&&but NOTauto&.autowill do a copy of that temporary,const auto&andauto&&will bind to that temporary and prolong its life until the end of the scope.auto&would NOT work here as l-value references cannot bind to temporary values.
-
auto&will be useful only if your (member) function foo returns a l-value reference -T& foo() -
Note that the expression on the right part of the equal operator can be more complicated than just a call to a function like foo. Anything that could assign
a_secret_variablecould belong there, likeauto&& a_secret_variable = std::pair(2, "bob");. -
Now that the compiler produced the anonymous variable, how do we obtain
xandyfrom it?- This will depend on the type of expression on the right.
- There are three cases: one for array types, one for simple types and one for types that act like
std::tuple.