# C++ Templates: How to Iterate through std::tuple: C++26 Packs and Expansion Statements

DevFeed: [C++ Templates: How to Iterate through std::tuple: C++26 Packs and Expansion Statements](<https://devfeed.tech/articles/c-templates-how-to-iterate-through-std-tuple-c-26-packs-and-expansion-statements-53317.md>)

Original publisher: [Read original article](<https://www.cppstories.com/2025/tuple-iteration-cpp26/>)

Published: 2025-10-12T00:00:00Z

Content type: tutorial

Language: en

Sources: [C++ Stories](<https://devfeed.tech/sources/c-stories.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [iteration](<https://devfeed.tech/topics/iteration.md>), [syntax](<https://devfeed.tech/topics/syntax.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [c-plus-plus-23](<https://devfeed.tech/tags/c-plus-plus-23.md>), [code](<https://devfeed.tech/tags/code.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [templates](<https://devfeed.tech/tags/templates.md>)

## AI overview

A tutorial on C++26 techniques for iterating over and transforming tuples. It explains structured binding packs and expansion statements, showing how they replace older index_sequence and std::apply approaches with simpler compile-time code.

## Source excerpt

In part 1 of this mini-series, we looked at the basics of iterating over a std::tuple using index_sequence and fold expressions. In part 2, we simplified things with std::apply and even created helpers like for_each_tuple and transform_tuple. So far, we used C++ features up to C++20/23... but now, in C++26, we finally get language-level tools that make tuple iteration straightforward and expressive. In this article, we'll explore two new techniques: Structured bindings can introduce a pack - P1061 - turn a tuple into a pack of variables. Expansion statements P1306 - the ultimate "compile-time loop" syntax. Structured binding packs Structured bindings have been around since C++17, but in C++26 they gained the ability to introduce packs. That means you can bind an arbitrary number of elements without spelling each out. For example: auto [head, ...rest] = std::tuple{1, 2, 3, 4}; // head == 1, rest... expands to (2, 3, 4) This feature gives us a way to expand tuples into parameter packs, and once we have a pack, we can use fold expressions and other familiar template techniques. Printing a tuple We can bind all elements of a tuple into a pack and then fold over it: #include <tuple> #include <iostream> #include <utility> template <typename Tuple> void print_tuple(const Tuple& t) { const auto& [...xs] = t; bool first = true; std::cout << "("; ((std::cout << (std::exchange(first, false) ? "" : ", ") << xs), ...); std::cout << ")"; } int main() { print_tuple(std::make_tuple(1, 2.2, "hello")); } See at Compiler Explorer This is a direct replacement for our old index_sequence or std::apply approaches - now written in plain C++26. (Of course, you can easily update it to work with std::print) Transforming a tuple We can also apply a transformation and create a new tuple: template <typename Tuple, typename Fn> auto transform_tuple(const Tuple& t, Fn&& fn) { const auto& [...xs] = t; return std::make_tuple(fn(xs)...); } std::tuple tp{1, 2, 3}; auto doubled = transform_tuple(tp, [](a