← Back to C++ Course | Chapter 16: Modern C++ | Lesson 7 of 9

C++ Structured Bindings

Structured bindings let you unpack the parts of a group into separate named variables in one go, like opening a gift set and naming each item. It is neat for pairs, structs and arrays.
Syntax
cpp
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?

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using the wrong number of names for the members.
  2. Forgetting & when you want to modify the original.
  3. Trying to bind to a type that does not support it.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.