C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 14 Aug 2025 10:42:28 -0700
On Wednesday, 13 August 2025 22:06:26 Pacific Daylight Time Oliver Hunt via
Std-Proposals wrote:
> <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>}

The equivalent of this in a constexpr for would be:

  for constexpr (size_t I = 0; I < sizeof...(xs); ++I)
      std::println("{}", xs...[I])

using pack indexing, but I think we'll all agree that the "range for" syntax
is nicer where possible.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Platform & System Engineering

Received on 2025-08-14 17:42:30