What is Structured Binding in C++?
Structure binding is a C++ feature that was added in C++17 that also allows the decomposition of a struct or tuple into individual variables, improving the conciseness of code. It can be used to make struct member access syntax simpler and lower the possibility of errors brought on by typos or incorrect indexing.
Syntax
The syntax of structure binding in C++ is as follows:
In the syntax for structure binding in C++, where “var1”, and “var2” are variable names, and “expression” is an expression that yields a struct or class. We use auto keywords to declare variables that are automatically created.
Modifiers
Modifiers can be used in pairing with structured bindings. Their syntax is mentioned below:
auto&& [a, b, c, ...] = expression;
The “&” or “&&” operator used in the declaration defines whether the binding is a lvalue or rvalue reference. A lvalue reference represented by “&” establishes a reference that may be used to change the values of the binding variables. Changes to the values made using the reference will be seen in the original structure or tuple.
The rvalue reference represented by “&&”, on the opposite hand, provides a reference that is limited to being utilized for reading the value of the variables included in the binding. It is handy for binding to transient objects or rvalues that need to be destroyed since it avoids making a replica of the object, this can be memory and time-consuming.
Example 1: Structure Binding in C++
The following is an example of C++ structure binding:
using namespace std;
struct cube
{
int x;
int y;
int z;
};
int main( )
{
cube c = { 10,20,30 };
auto[ x_coordinate, y_coordinate , z_coordinate ] = c;
cout << "X axis : " << x_coordinate << endl;
cout << "Y axis : " << y_coordinate << endl;
cout << "Z axis : " << z_coordinate << endl;
return 0;
}
In the above example, we declare a struct cube with three integers x, y, and z. The structure shows a cube in space. A variable c of type cube is created and initialized with values (10,20,30). In this code structure binding assigns values of members x, y, and z of struct to the individual variables x_coordinate, y_coordinate, z_coordinate respectively by using syntax auto[ x_coordinate, y_coordinate, z_coordinate ] = c. The output of the above is shown below:
Example 2: Structure Binding in C++ to Unpack a Struct
Following is an example of structure binding of unpacking a struct:
#include <string>
using namespace std;
struct Student {
string name;
int age;
};
int main() {
Student s{"Hamza", 32};
auto [name, age] = s;
cout << name << " is " << age << " years old." << endl;
return 0;
}
In the above code, a Student struct has two members: a name that is a string and an age that is an integer. Then, we create the Student object and assign initial values to each of its members. The members of s are then separated into the variable’s name and age using structural binding, and these values are then printed as in the screenshot below:
Example 3: Structure Binding in C++ With Modifiers
Below is an example of structure binding that makes use of a modifier to update the value of a variable as num1 and num2:
#include <tuple>
using namespace std;
int main() {
tuple<int, int> t{25, 20};
auto& [num1, num2] = t;
cout << "The value of num1 = " << num1 << ", num2 = " << num2<< '\n';
num1 = 30;
cout << "The changed value of num1 = " << num1 << ", num2 = " << num2 <<
'\n';
auto&& [num3, num4] =make_tuple(100, 250);
cout << "Now the value of num3 = " << num3 << ", num4 = " << num4 << '\n';
return 0;
}
In the preceding code, we build a tuple t and use the lvalue reference to link its components to num1 and num2. We then change the value of num1 to 30 and output the num1 and num2 values. Also build a temporary tuple via make_tuple(100, 250) and use a rvalue reference to tie its elements to num3 and num4. The values of num3 and num4 are then printed because num3 and num4 are coupled with a rvalue reference, they can’t be used to change the temporary tuple formed by make_tuple(100, 250). They are only capable of reading from it. When you execute the program, the following result will display on the screen:
Conclusion
Structure binding is a feature in C++ that unpacks multiple values of a struct or class into individual variables in a single expression, resulting in more concise, readable, and safer code. Structure binding using modifiers streamlines the process of changing values within structured objects.