C++ Structured Bindings
In this page:
auto [name1, name2] = pair_or_struct;
What is Structured Binding?
Structured bindings, introduced in C++17, let you unpack multiple values from a struct, pair, tuple, or array into individually named variables in a single declaration, instead of accessing each member one at a time through dot or index syntax.
Example: What is Structured Binding?
// Include std::cout and std::cin
#include <iostream>
#include <utility>
// Program execution starts in main()
int main() {
std::pair<int, int> point(3, 4);
auto [x, y] = point;
// Print to the console with cout
std::cout << x << "," << y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Binding to Struct Members
For a struct, the syntax auto [a, b] = myStruct; unpacks its public members in declaration order into the variables a and b. The number of names inside the brackets must exactly match the number of members being unpacked, or the compiler rejects it.
Example: Binding to Struct Members
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; int y; };
// Program execution starts in main()
int main() {
Point p{3, 4};
auto [a, b] = p;
// Print to the console with cout
std::cout << a << "," << b << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Binding to Arrays
The same bracket syntax works on a fixed-size C-style array -- auto [x, y, z] = myArray; unpacks each of its three elements into separate variables, which is often more readable than repeatedly indexing the array by number.
Example: Binding to Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
auto [x, y, z] = arr;
// Print to the console with cout
std::cout << x << " " << y << " " << z << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using References in Bindings
By default, structured bindings copy each unpacked value into its new variable, so changes to the bound variable don't affect the original struct or array.
Declaring the binding as a reference -- auto& [a, b] = myStruct; -- instead binds directly to the original members, so modifications propagate back.
Example: Using References in Bindings
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; int y; };
// Program execution starts in main()
int main() {
Point p{1, 2};
auto &[x, y] = p;
x = 100;
// Print to the console with cout
std::cout << p.x << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using const References
When you want to unpack values without copying but also want to guarantee they can't be accidentally modified, const auto& [a, b] = myStruct; gives you a read-only reference binding -- the best choice when you're only inspecting a large struct's fields.
Example: Using const References
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; int y; };
// Program execution starts in main()
int main() {
Point p{5, 6};
const auto &[x, y] = p;
// Print to the console with cout
std::cout << x << " " << y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using the wrong number of names for the members.
- Forgetting
&when you want to modify the original. - Trying to bind to a type that does not support it.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: