structure binding is a way to decompose structure/array similar to python unpacking.
In this example, u and v types are aliases for the structure elements i and s of anonymous variable that copied structure m.
#include <string>
#include <iostream>
using namespace std;
struct Mystruct
{
int i = 0;
std::string s;
};
int main()
{
{
Mystruct m;
auto [u, v] = m;
}
return 0;
}
When using reference, now u and v are reference to m1.i and and m1.s
int main()
{
{
Mystruct m1{2, "hello"};
auto &[u, v] = m1;
cout << m1.i << endl;
u = 1000;
cout << m1.i << endl;
}
return 0;
}
The structure binging is handy while “unpacking” containers like array, tuple and pair. In this example, tuple is bound to 2 variables
int main()
{
tuple<int, float> mytup{1, 2.2};
auto [a, b] = mytup;
cout << a << " " << b << endl;
return 0;
}