C++ Logo

std-proposals

Advanced search

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

From: Oliver Hunt <oliver_at_[hidden]>
Date: Thu, 14 Aug 2025 00:03:59 -0700
> On Aug 13, 2025, at 11:52 PM, Ivan Lazaric <ivan.lazaric1_at_[hidden]> wrote:
>
> But the "size of the container" is constexpr, like the condition in if constexpr, and unlike a regular for loop
> template for is codegen based on sizeof...(xs), if constexpr is codegen based on the condition (roughly)
>

The value of the enumerand in each iteration is not constant, unlike the value of the condition in constexpr if, and that’s what I would argue is the correct equivalence.

But I think reasonable minds can disagree on the exact equivalence (I can see your pov, but to me it makes more sense to think about values), so the other point is that the number of iterations is _not_ a constant, as the iteration count can depend on any number of factors (either the values of the thing being iterated, or any other program state), e,g:

template for (….) {
  ...
  if (some arbitrary condition) { return/break/throw }
  ...
}


Cheers,
  Oliver

Received on 2025-08-14 07:04:11