Date: Wed, 13 Aug 2025 22:06:26 -0700
> On Aug 13, 2025, at 9:50 PM, Oliver Hunt <oliver_at_[hidden]> wrote:
>
>
>
>> On Aug 13, 2025, at 9:41 PM, 叶易安 via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> 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` ?
>>
>
> template for is not a constexpr, so this would be incorrect.
>
> `if constexpr` is an if that is guaranteed to be evaluated at compile time, template for is just a for loop where each “iteration” may have a different type. The type unrolling occurs at compile time, but the body of the for loop is not evaluated at compile time, which is what constexpr would be saying.
Sorry was written super unclearly.
In `If constexpr` the condition is a constexpr, whereas in `template for` the object being iterated is not required to be a constexpr, and because the body is not a constant it could in principle have runtime controlled continue, break, return, throw, (goto? :-O), etc impacting the number of iterations (I’m not sure if the current proposal permits that but if not it’s an obvious future step).
For example template for permits (directly citing p1306)
template <class... Ts>
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-2>void print_all(Ts... xs) {
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-3> template for (auto elem : {xs...}) {
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-4> std::println("{}", elem);
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-5> }
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-6>}
Which is clearly not a constexpr. The equivalent constexpr if - as if we were to make `template for` become `for constexpr` - would be
template <class T>
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-2>void print_x(T x) {
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-3> if constexpr (x) {
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-4> std::println("{}", x);
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-5> }
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-6>}
As you can see the analogous syntax would not be a valid constexpr if.
—Oliver
>
>
>
>> On Aug 13, 2025, at 9:41 PM, 叶易安 via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> 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` ?
>>
>
> template for is not a constexpr, so this would be incorrect.
>
> `if constexpr` is an if that is guaranteed to be evaluated at compile time, template for is just a for loop where each “iteration” may have a different type. The type unrolling occurs at compile time, but the body of the for loop is not evaluated at compile time, which is what constexpr would be saying.
Sorry was written super unclearly.
In `If constexpr` the condition is a constexpr, whereas in `template for` the object being iterated is not required to be a constexpr, and because the body is not a constant it could in principle have runtime controlled continue, break, return, throw, (goto? :-O), etc impacting the number of iterations (I’m not sure if the current proposal permits that but if not it’s an obvious future step).
For example template for permits (directly citing p1306)
template <class... Ts>
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-2>void print_all(Ts... xs) {
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-3> template for (auto elem : {xs...}) {
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-4> std::println("{}", elem);
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-5> }
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-6>}
Which is clearly not a constexpr. The equivalent constexpr if - as if we were to make `template for` become `for constexpr` - would be
template <class T>
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-2>void print_x(T x) {
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-3> if constexpr (x) {
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-4> std::println("{}", x);
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-5> }
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html#cb4-6>}
As you can see the analogous syntax would not be a valid constexpr if.
—Oliver
Received on 2025-08-14 05:06:48