CKTIS / C++
Structured binding declaration
A structured binding names elements of an array or a suitable class object. The declaration form alone does not determine whether writing through a binding changes an original element: when plain `auto` copies a `std::tuple<int&, int&>`, the bindings still refer to the same integers.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
auto [a, b] = expression;
auto& [a, b] = expression;
const auto& [a, b] = expression;A structured binding declaration introduces new names through auto [a, b] = expression;. It is a declaration, not assignment to existing variables; an initializer is mandatory.
It supports arrays, classes using the tuple protocol (std::tuple_size, std::tuple_element, get), and suitable classes with accessible non-static data members. Members are bound in declaration order. In C++23, the number of names must match the number of decomposed elements.
The declaration creates a hidden variable. For the array shown, auto copies the elements, whereas auto& binds the original. const auto& allows reading without copying the array. The names refer to elements, rather than independently copied variables.
This entry targets C++23. The linked draft evolves and also contains newer extensions; do not assume those are available in C++23.
Examples
#include <iostream>
int main() {
int values[2] = {3, 7};
auto [a, b] = values;
auto& [x, y] = values;
a = 10;
x = 20;
std::cout << a << ' ' << b << '\n';
std::cout << values[0] << ' ' << y << '\n';
}
Result
10 7 20 7
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.