C++ Logo

std-proposals

Advanced search

Re: [std-proposals] for-loops revision (simplification and new syntax)

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Mon, 10 Feb 2025 15:49:14 +0000
On Mon, 10 Feb 2025 at 15:13, Pau Miquel Montequi Hernandez <
pau.montequi_at_[hidden]> wrote:

> @Marcin Jaczewski
>
> Allow code like:
>>
>> for (int i : std::views::iota(1, 10))
>> std::cout << i << ' ';
>>
>> Other versions you can create helpers in a similar way.
>
>
> This example requires to include a header instead of being an intrinsic
> property of the for loop. Also is way more verbose than for (int i = 1 :
> 10) my hope is to make things simpler and easier to read without losing
> functionality.
>
> @Jonathan Wakely
>
> You can do this today with a structured binding:
>
> for (auto [b,e,o] = std::tuple(in, in + size, out); b != e; ++b, ++o)
>> *o = std::tolower(*b);
>
>
> True, but isn't creating a tuple to just decompose it
> excessively verbose? Not to mention the need to include a header;
>

That's considerably easier than changing the language, and works today :-)



> also we lose the expressivity of separating each declaration, on my
> opinion each part separated on different declarations implies
> intentionality, also more control on the resulting type, i.e:
>
> template <auto size>
>> void lcase(const char (&in)[size], char (&out)[size])
>> {
>> for (const char *b = in; const char *const e = in + size; char *o =
>> out; b != e; ++b, ++o) // Three different explicit types
>> *o = std::tolower(*b);
>> }
>
>
>
> Missatge de Jonathan Wakely <cxx_at_[hidden]> del dia dl., 10 de febr.
> 2025 a les 15:31:
>
>>
>>
>> On Mon, 10 Feb 2025 at 14:18, Pau Miquel Montequi Hernandez via
>> Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>>>
>>> More flexible for loop initialization, allowing multiple init statements:
>>>
>>>> template <auto size>
>>>> void lcase(const char (&in)[size], char (&out)[size])
>>>> {
>>>> for (auto b = in, e = in + size; auto o = out; b != e; ++b, ++o)
>>>>
>>>
>> You can do this today with a structured binding:
>>
>> for (auto [b,e,o] = std::tuple(in, in + size, out); b != e; ++b, ++o)
>>
>>
>>> *o = std::tolower(*b);
>>>> }
>>>
>>>
>>>
>
> --
> Pablo Miguel Montequi Hernández.
>

Received on 2025-02-10 15:49:33