C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Iterating over a parameter pack for...

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 5 Dec 2023 11:02:38 +0100
wt., 5 gru 2023 o 03:07 Julien Villemure-Fr├ęchette via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> Recursive evaluation of consteval functions seem even more natural than using control flow structures for traversing parameter packs.
> Recursion termination is done by providing a more specialized overload of the function.
>

There are already proposals for change like this.
You can even see it in this paper as side note in one of examples:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2996r0.html#enum-to-string

`template for` has a big advantage over the current approach as it is
dramatically simpler than a set of overloads.
Overall my pet peeve would be mixing `template for` with `swich`:
```
switch (x)
{
    template for (constexpr auto a : list)
    {
        // this line is effective copy pasted multiple times
        // with different `a` from each iteration of `template for`
        case a: return foo<a>();
    }
}
```
But I do not know if something like this would even be included in the standard.

>
> On December 4, 2023 7:24:31 p.m. EST, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> Fold expressions are fantastic and it's great what we can do with
>> them. Sometimes though, some really simple operations turn into a
>> really complicated fold expression involving a template lambda. That
>> which could have been a very simple 'for' loop gets turned into a
>> pretty elaborate fold expression, possibly with a lambda and some
>> extra helper classes and helper functions to get the job done.
>>
>> I'm not the first person to suggest that we should have "for..." in
>> the language, and so just to stir things up again, here's 3 possible
>> uses of it:
>>
>> (1) Iterating over compile-time constants in a parameter pack
>>
>> #include <cstddef> // size_t
>> template<std::size_t... indices>
>> consteval bool contains_multiple_of_eight(std::size_t const arg)
>> {
>> for... ( i : indices )
>> {
>> if ( 0u == (i % arg) ) return true;
>> }
>>
>> return false;
>> }
>>
>> (2) Iterating over runtime arguments
>>
>> template<typename... Params>
>> consteval bool contains_at_least_one_true(Params&&... args)
>> {
>> for... ( arg : args )
>> {
>> if constexpr ( requires { static_cast<bool>(arg); } )
>> {
>> if ( static_cast<bool>(arg) ) return true;
>> }
>> }
>>
>> return false;
>> }
>>
>> (3) Iterating over types
>>
>> template<typename... Params>
>> consteval bool
>> all_implicitly_convert_to_false_after_default_initialisation(void)
>> {
>> for typename... ( P : Params )
>> {
>> if constexpr ( !requires { static_cast<bool>( P{} ); } )
>> return false;
>> }
>>
>> return true;
>> }
>>
>> I'm not trying to argue here that these three examples are impossible
>> to implement with normal fold expressions. I'm saying that we could
>> write much more simple code if we had "for..." in the language.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-12-05 10:02:49