Structured bindings
A structured binding unpacks an array, a pair-like type, or a struct into named aliases: auto [key, value] = *it;. The names are bound to the elements; they are not a new tuple.
Detailed Explanation
You look up a user in a map and get an iterator to a std::pair<const std::string, User>. The pre-C++17 way was it->first and it->second, which says nothing about "name" and "user". Structured bindings (C++17) let you write auto [name, user] = *it; and then use those words.
The declaration is auto [a, b, c] = expr; or with references auto& [a, b, c] = expr; or const auto& [a, b, c] = expr;. You may also write auto&&. The number of names must match the number of elements. The compiler creates a hidden variable e initialised from expr (copy, move, or bind, depending on the auto form), then binds each name to a member or element of e.
Three kinds of type work:
- C arrays.
int xs[] = {1, 2, 3}; auto [a, b, c] = xs;copies the array intoeand bindsa,b,cto the copy.auto& [a, b, c] = xs;binds to the original. - Tuple-like types. Anything with
std::tuple_size,std::tuple_element, andget<N>.std::tuple,std::pair,std::arrayqualify. You can opt your own type in by specialising those templates. - Aggregates. A class with only public non-static data members, no user-declared constructors that get in the way of aggregate rules, and no anonymous unions. Members are taken in declaration order.
The binding is an alias, not a copy of the element on top of the copy of e. If you wrote auto [a, b] = p;, e is a copy of p, and a names e.first. If you wrote auto& [a, b] = p;, e is a reference to p, and assigning a assigns p.first.
Bit-fields can be bound. The binding is not an lvalue you can take the address of, because bit-fields have no address.
C++20 added [=, this] style captures; C++26 is adding attributes on bindings and more packing features. In C++17/20 the rule you need is: use const auto& when you are only reading a container element, auto& when you will assign back, and auto when you want a snapshot of the whole object.
Functions that return two values become readable: auto [ok, port] = parsePort(text);. Prefer a small struct with named members over a std::pair when the types are the same (pair<int, int> of "width" and "height" is how bugs are born). Structured bindings still help even with a struct: auto [width, height] = size;.
You cannot write a type on the binding names. auto [int x, int y] is invalid. Qualifiers go on the auto. If one element needs a different constness, bind the whole object and then take references yourself.
Code Examples
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> ages{{"ada", 36}, {"al", 46}};
for (const auto& [name, age] : ages) {
std::cout << name << '=' << age << '\n';
}
// prints:
// ada=36
// al=46
auto [it, inserted] = ages.insert({"ada", 99});
std::cout << std::boolalpha << inserted << ' ' << it->second << '\n';
// prints: false 36
}#include <iostream>
#include <string>
struct Size {
int width;
int height;
};
Size window() { return Size{1280, 720}; }
int main() {
auto [w, h] = window(); // copies the struct, binds w and h
std::cout << w << 'x' << h << '\n'; // prints: 1280x720
}Common Gotchas
The name count must match
three members and two names is a compile error. There is no rest binding in C++17/20.
Order is declaration order
renaming a struct member does not break the binding. Reordering members does, silently. Prefer named members when the struct is not obviously positional.
Bindings are not regular variables in some contexts
you cannot `static` them independently, and until later standards some attributes and captures were awkward. Treat them as names for elements, not as first-class locals you decorate.