r/cpp • • Mar 08 '26

Exploring Mutable Consteval State in C++26

https://friedkeenan.github.io/posts/exploring-mutable-consteval-state/
83 Upvotes

14 comments sorted by

View all comments

Show parent comment

3

u/friedkeenan Mar 09 '26 edited Mar 09 '26

Yep, and you can even use std::views::indices in C++26 to simplify usage of std::views::iota too.

Maybe I should have put this as a blurb in the blogpost, but you don't need to break the expand_loop just based off of the loop index. It could be any arbitrary compile-time condition.

So you could for instance break the loop once a string you're building is of a certain length: https://compiler-explorer.com/z/TboszP1K3

You could precalculate this sort of thing, before you set up your loop, but then you might end up doing some amount of duplicate work, and the code might be annoyingly indirected.

With this though, you just get to figure it out as you step through your loop. Barring the somewhat arcane interface, expand_loop might lead to more straightforward code.

Though yes, I certainly don't think this should overtake many usages of template for. That's a great and expressive language feature for sure, and this is... maybe not so much, lol.