C++ Tutorial

Structured Bindings

Decomposing tuples, pairs, and structs (C++17)

What are Structured Bindings?

Introduced in C++17, Structured Bindings are a powerful tool that allows you to "decompose" complex objects like std::pair, std::tuple, or even simple structs into several individual variables in one single line. This is much more readable than using .first or .second.
cpp
#include <map>
#include <string>

std::map<int, std::string> users = {{1, "Alpha"}, {2, "Beta"}};

// Modern way!
for (const auto& [id, name] : users) {
  std::cout << id << ": " << name << std::endl;
}

How to use Structured Bindings

You use the auto keyword followed by a list of variables inside square brackets [].

1. With Structs

Decompose a basic struct directly into named variables.
cpp
struct Point { int x, y; };
Point p{10, 20};

// Decompose into x and y!
auto [x, y] = p;
std::cout << x << ", " << y << std::endl; // 10, 20

2. With std::pair and std::tuple

This is extremely common when functions return multiple values.
cpp
#include <tuple>

std::tuple<int, string, bool> getStatus() {
    return {200, "OK", true};
}

// Cleanly unpack all three values!
auto [code, msg, success] = getStatus();

Comparisons: Access by Reference vs. Copy

Wait! By default, auto [x, y] = obj will create copies of the data. To avoid this (especially for large strings or vectors), use a reference.
  • auto [x, y] = obj: Creates new copies.
  • auto& [x, y] = obj: Creates references (Modifies the original!).
  • const auto& [x, y] = obj: Creates read-only references (High performance!).

Summary

FeatureSyntaxBehavior
Simpleauto [a, b]Unpack into new variables
Referenceauto& [a, b]Unpack as references
Constantconst auto& [a, b]Read-only access
Best ForLooping over maps, returning multiple values
ModernC++17 required!
CleanerNo more .first and .second!
DeduceCompiler automatically calculates the types
LimitsYou must unpack ALL members of the struct!
ArraysAlso works with fixed-size arrays!
PerformanceZero cost compared to manual access
ReturnIdeal for std::map::insert() return values
NestedUse inside loops for massive readability boost
SafetyPrevents forgetting to access the second half of a pair
AliasEffectively gives data members a local nickname
StandardReplacing the older std::tie() approach
StructsWorks only with public data members
OrderMust follow the order defined in the struct!