This is not a bad idea per-say, but there are other features, libraries and proposals that cover this. Here’s an incomplete list:

Boost.Hana (and others)
The paper Christof linked
Template recursion (see [1])


A static for loop:

template <int First, int Last, typename Lambda>
inline void static_for(Lambda const& f)
    if constexpr (First < Last)
         f(std::integral_constant<int, First>{});
         static_for<First + 1, Last>(f);


On May 1, 2022, at 4:23 AM, Jeremy via Std-Proposals <> wrote:

The only real use I can imagine for something like this is template metaprogramming, as with if-constexpr, to achieve similar goals to std::integer_sequence. Something like,


template<int N>

void foo() { … }


void bar() {

    for constexpr(int i = 0; i < 10; i++) {





But the semantics wouldn’t quite make sense here.


The paper Christof Meerwald just linked seems a good way to approach this.


From: Std-Proposals <> On Behalf Of Abdullah Qasim via Std-Proposals
Sent: Sunday, May 1, 2022 03:51 AM
To: Jason McKesson via Std-Proposals <>
Cc: Abdullah Qasim <>
Subject: [std-proposals] constexpr loops



for constexpr ()
for consteval ()
while constexpr ()
while consteval ()
do constexpr {} while ()
do consteval {} while ()
for consteval (int i{ 0 }; i < 1’000’000; ++i) {
    cout << "LOL”;


Runtime speed is FAST, w/ these loops.


Easy to implement, too.

Std-Proposals mailing list