r/cpp • • Jul 20 '26

C++26: what is “template for”? Learning with simple example.

https://techfortalk.co.uk/2026/07/20/c26-what-is-template-for/

I have been exploring C++26 in bits and bobs and this one is about template for - a c++26 feature which I find useful. This is a short article explaining how to use this feature with the help of a simple example.

74 Upvotes

43 comments sorted by

View all comments

37

u/mercury_pointer Jul 21 '26

Seems strange that the syntax isn't

for constexpr

To mirror

if constexpr

27

u/SyntheticDuckFlavour Jul 21 '26

Yeah template seem like an odd choice for this, but I guess the rationale that is the item type at each "loop" iteration may be different.

  template <class... Ts>
  void doStuffWithAll(Ts... ts) {
    template for (auto t : {ts...}) {
      doStuff(t);
    }
  }

27

u/cmeerw C++ Parser Dev Jul 21 '26

The template keyword is there to let you know that you are now in a template context and might have to use template or typename, e.g.

struct B {
    template<int> void f();
};

struct C {
    B b;
};

void f(C c)
{
    template for (auto v : c) {
        v.template f<1>();
    }
}

8

u/friedkeenan Jul 21 '26
for constexpr (constexpr auto Elem : Elements)

Would also read as kind of busy to me.

1

u/Life_Sink9598 Jul 22 '26
template<int> void f();

Huh, I've never seen this syntax before, what does that mean?

2

u/cmeerw C++ Parser Dev Jul 22 '26

A function template declaration with a non-type template parameter (nowadays called constant template parameter) - that's been around since C++98

2

u/Life_Sink9598 Jul 23 '26

You know what? I've used that feature a lot haha :-)

1

u/PunctuationGood Jul 23 '26

The mere fact that it wasn't written in two lines like so is what threw me off, I'm guessing you as well... :D

template</*...*/>
void f();

1

u/Life_Sink9598 Jul 23 '26

Haha, that's exactly what it was, but I was a bit too embarrassed to admit it!

15

u/Aware-Preference-626 Jul 21 '26

I think it's because it's not technically a `comptime for loop`; it's unpacking/iterating variadic template parameters, so `template for` is the right wording here

5

u/AvidCoco Jul 21 '26

Wouldn’t that imply the thing being looped has to be constexpr? Whereas template for works with, for example, tuples. Idea being that it acts on types, not values.

7

u/Tringi github.com/tringi Jul 21 '26

I'd prefer static if and static for but that ship has sailed a long time ago.

8

u/_Noreturn Jul 21 '26

I like if constexpr spelling more

2

u/Dubbus_ Jul 21 '26

Anyone else often mistype it as if (constexpr) some_condition for some reason

2

u/fdwr fdwr@github 🔍 Jul 21 '26 edited Jul 23 '26

With condition outside the parentheses? No, but I often type constexpr if (expression) by accident, and I get that if constexpr (expression) is consistent with east-const like int const x, but it still confuses me because adjective modifiers come before the thing in English, and other modifiers typically flow static int x, not int static x.

3

u/_Noreturn Jul 21 '26

I think of it as if with constexpr condition that's how I read it

2

u/fdwr fdwr@github 🔍 Jul 21 '26

That's a helpful mnemonic to remember the order.

2

u/Dubbus_ Jul 21 '26

Come to think of it, why is it not constexpr if? Is it a grammar issue?

2

u/_Noreturn Jul 22 '26

saying else constexpr if doesn't sound great the else and if are seperated.

There is no grammar issue afaik

2

u/Dubbus_ Jul 22 '26

Why not constexpr else if? I always found it a bit weird that there is no constexpr on the else clause of an if constexpr {} else statement.

2

u/_Noreturn Jul 22 '26 edited Jul 22 '26

because there is no else if in the C++ grammar as a single construct

This is C++ grammar for if statement

statement: buncha things
                  | if-statement
if-statement:
    if [constexpr] ( condition ) statement [else statement]

[] means optional.

as you can see else mentions "statement" and "statement" mentions "if-statement" this is recursion when you do


if (c1)
else if(c2)
else 
// is just this

if (c1) {
       ;
} else {
    if (c2)   {
        ;
    } else {
        ;
    }
}

so if you wanted "constexpr else if" you would need to create a new grammar thing for it to work but with "if" it just works naturally due to recursion.

if you allowed "constexpr" before else as a speciality then it would allow "constexpr else if constexpr" which is mirrored but awkward

2

u/fdwr fdwr@github 🔍 Jul 22 '26 edited Jul 23 '26

because there is no else if in the C++ grammar as a single construct

Hehe, even after 20+ years of C++, I still sometimes use the preprocessor for a few things (typing #if, #elif...) and then right afterward try typing elif outside the preprocessor context, only to get a build error. Oh yeah, that odd inconsistency arises again 😉.

→ More replies

2

u/retro_and_chill Jul 21 '26

Because it can actually look over non-constant expressions like tuples or fixed size arrays