I'm trying to understand structured binding introduced in C++17. The explanation on cppreference is not obvious to me, but it looks like
cv-auto ref-operator [x, y, z] = ...
is roughly equivalent to (not to consider array case)
cv-auto ref-operator unique_name = ...
#define x unique_name.member_a
#define y unique_name.member_b
#define z unique_name.member_c
The key point here is that x y z
are not independently defined variables, but just aliases of the return value members. And cv-auto ref-operator
applies to the return value, not the aliases (the syntax may be misleading here). For instance, see the cppreference example
float x{};
char y{};
int z{};
std::tuple<float&,char&&,int> tpl(x,std::move(y),z);
const auto& [a,b,c] = tpl;
// a names a structured binding of type float& that refers to x
// b names a structured binding of type char&& that refers to y
// c names a structured binding of type const int that refers to the 3rd element of tpl
If a b c
are independently defined variables, with const auto&
applying to them, c
cannot be of type const int
.
From a practical point of view, what are the key points this analogy failed to catch?
See Question&Answers more detail:os