CKTIS / C++
Structured bindings: `auto [a, b, c]` vs. `std::get` and `std::tie`
Declare names for tuple elements with structured bindings. Learn when they refer to a copy or the original, and how they differ from `std::get` and `std::tie`.
Version: ISO C++23; implementation support may vary
Syntax
auto [a, b, c] = t;
auto& [a, b, c] = t;
const auto& [a, b, c] = t;
// Alternative declarations: use different names or separate scopes.
std::get<0>(t);
std::tie(first, std::ignore, last) = t;auto [a, b, c] = t; is a structured binding declaration: it introduces three new names for successive elements. Structured bindings date to C++17; this entry follows ISO C++23, and implementation support may vary.
In C++23 the number of names must match the number of elements or members being decomposed; otherwise the program is ill-formed. Built-in arrays use index order, tuple-like types use the std::tuple_size, std::tuple_element, and get protocol, and other eligible classes use accessible non-static data members in declaration order. The tuple-like protocol isn't an explicit call to std::get: it uses a qualifying member template get<I>() or looks up a free get<I> by argument-dependent lookup (ADL) alone.
auto, &, and const apply to a hidden variable. For t in the example, auto copies the tuple, auto& binds to the original, and const auto& lets you read its elements without copying but not modify them through those names. Copying a tuple of references does not copy the objects they refer to. Don't use a binding after the object it refers to has been destroyed.
For std::tuple, std::get<I>(t) from <tuple> returns a reference to one element; the template index is zero-based and must be in range. std::get<T>(t) instead requires T to occur exactly once. std::tie makes a tuple of references to existing objects; = assigns through those references, and std::ignore skips a position. A structured binding declares names and does not support skipping a position this way.
Examples
#include <iostream>
#include <tuple>
int main() {
std::tuple<int, int, int> t{1, 2, 3};
auto [a, b, c] = t;
auto& [x, y, z] = t;
a = 9;
x = 7;
int first = 0;
int last = 0;
std::tie(first, std::ignore, last) = t;
std::cout << "copy: " << a << '\n';
std::cout << "get: " << std::get<0>(t) << '\n';
std::cout << "tie: " << first << ' ' << last << '\n';
}Result
copy: 9 get: 7 tie: 7 3
`a = 9` changes only the copy; `x = 7` changes the first element of `t`. So `std::get<0>(t)` gives `7`, and assignment through `std::tie` writes `7` to `first` and `3` to `last`, skipping the middle element.
Related lessons
- C++ · Lesson 32 · Intermediate · Version: 2
Sources
- WG21 N4950 — Working Draft, Standard for Programming Language C++ (C++23; [dcl.struct.bind], [tuple.elem], [tuple.creation], [tuple.assign])
- Structured binding declarations [dcl.struct.bind] — current working draft; use N4950 for C++23 wording
- Tuple element access [tuple.elem]
- Tuple creation functions [tuple.creation]
- Tuple assignment [tuple.assign]