C++26 Expansion Tricks
Published on: 2025-05-25 22:51:12
P1306 gives us compile time repetition of a statement for each element of a range - what if we instead want the elements as a pack without introducing a new function scope?
In this blog post we’ll look at the expand helper, expansion statements and how arbitrary ranges can be made decomposable via structured bindings to reduce the need for IILEs.
Element-wise expansion
The expand pattern
The reflection features introduced in P2996 by themselves are sufficient to iterate over a compile time range. The paper introduces a helper expand for this purpose, here’s a slightly modified version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 template < auto ... Elts > struct Replicator { template < typename F > constexpr void operator >> ( F fnc ) const { ( fnc . template operator () < Elts >(), ...); } }; template < auto ... Elts > constexpr inline Replicator < Elts ... > replicator {}; template < std :: ranges :: range R > consteval std :: meta :: info expand ( R const & range ) { std ::
... Read full article.