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
‘consteval’
Template recursion (see [1])

[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);
      }
}

WL

On May 1, 2022, at 4:23 AM, Jeremy via Std-Proposals <std-proposals@lists.isocpp.org> 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++) {

        foo<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 <std-proposals-bounces@lists.isocpp.org> On Behalf Of Abdullah Qasim via Std-Proposals
Sent: Sunday, May 1, 2022 03:51 AM
To: Jason McKesson via Std-Proposals <std-proposals@lists.isocpp.org>
Cc: Abdullah Qasim <iamminecraftredstoner@outlook.com>
Subject: [std-proposals] constexpr loops

 

 

for constexpr ()
for consteval ()
 
while constexpr ()
while consteval ()
 
do constexpr {} while ()
do consteval {} while ()
 
example:
 
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
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals