C++

Structure Binding in C++

Structure binding allows you to extract the elements from a struct or class and assign them to different variables without having access to each member separately. Structure binding is used to eliminate the need for manual element extraction and improves the readability and conciseness of code by permitting the simultaneous unpacking of numerous elements of a struct or class into distinct variables. In this tutorial, we will discuss working, syntax, and examples of structure binding in C++.

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:

auto [var1, var2,...] = expression;

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;

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:

#include <bits/stdc++.h>

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 <iostream>

#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 <iostream>

#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.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.