<tuple> provides std::tuple<T1, T2, ...>, a heterogeneous fixed-size container holding values of different types. Use std::get<I>(t) to access by index or std::get<T>(t) to access by type. C++17 structured bindings (auto [x, y] = tuple) make tuples convenient.
Tuples are useful for returning multiple values from a function without defining a struct.
This example returns multiple values of different types from a function via tuple, using structured bindings to unpack them.
// compile: g++ -std=c++17 -o tupleexample tupleexample.cpp // run: ./tupleexample // description: tuple creation and unpacking #include <tuple> #include <iostream> #include <string> std::tuple<int, double, std::string> fetch_data() { return {42, 3.14, "hello"}; } int main() { auto [i, d, s] = fetch_data(); std::cout << "int: " << i << ", double: " << d << ", string: " << s << "\n"; return 0; }