C++ Logo

std-proposals

Advanced search

[std-proposals] Some question on C++26 `template for` syntax

From: 叶易安 <shyeyian_at_[hidden]>
Date: Thu, 14 Aug 2025 12:41:41 +0800
C++26 `static reflection` proposal wanna to bring a syntax called `template
for`. We know that C++17 introduced one similar thing called `if
constexpr`, So why not make the style unified and call it `for constexpr` ?


Reasons:

- They both decorate a flow-control keyword (`if` vs `for`)

- They both indicate (and only indicate) that the variable in the
flow-control section is constexpr (`if constexpr (**var**)` vs `for
constexpr (**var** = ...; var < ...; ...)`)

- Style are more unified.

- May (potentially) expand into more future syntaxes within unified style:

- e.g. `switch constexpr(^^type): case ^int: return 1; case ^^double:
return "two"; case ^^long double: return 3.0`


Some examples might be like below:

```cpp

template <int N> func();

if constexpr (true)

for constexpr (int i = 0; i < 42; ++i)

func<i>(); // Expanded into func<0>(), func<1>(), ..., func<41>()

```


```cpp

template <class... Ts>

void func()

{

for constexpr (auto type : std::meta::member_data_of(^^A))

std::println("{}", std::meta::name_of(type)); // Reflect the data-members

}

```


Thank you! :)

Received on 2025-08-14 04:41:55