Structured Binding in C++
Structured binding is one of the newest features of C++17 that binds the specified names to subobjects or elements of initializer. In simple words, Structured Bindings give us the ability to declare multiple variables initialized from a tuple or struct.
The main purpose of Structured Bindings in C++ 17 is to make the code clean and easy to understand. Like a reference, a structured binding is an alias to an existing object. Unlike a reference, the type of a structured binding does not have to be a reference type.
Here is an example:
#include <bits/stdc++.h> using namespace std; struct Point { int x; int y; }; int main( ) { Point p = { 1,2 }; // Structure binding auto[ x_coord, y_coord ] = p; cout << "X Coordinate : " << x_coord << endl; cout << "Y Coordinate : " << y_coord << endl; return 0; }
with output:
X Coordinate : 1 Y Coordinate : 2
Syntax
Ths syntax for structured binding is:
auto ref-operator(optional)[identifier-list] = expression; // Or auto ref-operator(optional)[identifier-list]{expression}; // Or auto ref-operator(optional)[identifier-list](expression);
Where the parameters mean:
- auto : auto
- ref operator : either & or &&
- identifier-list : List of comma separated variable names.
- expression : An expression that does not have the comma operator at the top level (i.e, an assignment-expression), and has either array or non-union class type.
Applications/Examples
Structured Binding can be used with arrays to get the elements from the array. In this case, E is an array type, hence the names are bound to the array elements. Below is the implementation to show the same:
#include <bits/stdc++.h> using namespace std; int main() { int arr[3] = { 1, 2, 3 }; // Here, E is an array type, hence the // names are bound to the array elements. auto[x, y, z] = arr; cout << x << " " << y << " " << z << endl; return 0; }
Output:
1 2 3
Note. The number of identifiers in the identifier list must be equal to the number of elements in the array. If the number of identifiers in the identifier list is less, then either a compile time error or design time error may occur. This means that we cannot take the specific set of elements from the array. A more practical example for using the structured bindings is as follows:
#include <bits/stdc++.h> #include <map> using namespace std; int main() { // Creating a map with key and value // fields as String map<string, string> sites; sites.insert({ "GeeksforGeeks", "Coding Resources" }); sites.insert({ "StackOverflow", "Q-A type" }); sites.insert({ "Wikipedia", "Resources + References" }); for (auto & [ key, value ] : sites) { cout << key.c_str() << " " << value.c_str() << endl; } return 0; }
Output:
GeeksforGeeks Coding Resources StackOverflow Q-A type Wikipedia Resources + References
Note: Qualifiers such as const and volatile can be used along with type to make the declaration variable constant or volatile.