CKTIS / C++
std::tuple — a tuple of elements with fixed types
std::tuple from <tuple> groups a fixed number of elements whose types may differ. std::get provides access, while structured bindings let you give the elements local names.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <tuple>
std::tuple<Types...> name{values...};
std::get<I>(name);
std::get<T>(name);std::tuple<Types...> from <tuple> stores a fixed number of elements. Each template argument specifies the type of one position; types may repeat or differ. The number and types of positions are fixed at compile time, but values can change when their types and qualifiers allow it.
std::get<I>(t) returns a reference to the element at zero-based index I. The index must be a compile-time constant within range; an invalid index is a compilation error, not an exception. std::get<T>(t) selects an element by type, but requires T to occur exactly once.
In the example, std::get<0>(item) changes the first element from 3 to 5. The declaration auto& [count, label] = item; introduces names referring to the original tuple's elements. In contrast, auto [count, label] = item; would, in this example, create a tuple copy and bind the names to its elements.
The example targets compilation with -std=c++23. The displayed code was run unchanged in an isolated environment, and its standard output was confirmed to match the stated output exactly. This check does not attest to the production runtime. The eel.is source is an evolving working draft rather than a frozen C++23 publication; this explanation is limited to functionality available in C++23.
Examples
#include <iostream>
#include <tuple>
int main() {
std::tuple<int, char> item{3, 'A'};
std::get<0>(item) += 2;
auto& [count, label] = item;
std::cout << count << ' ' << label << '\n';
}
Result
5 A
The displayed code was run unchanged in an isolated environment. Its standard output matched the stated output exactly.