C++ Logo

std-proposals

Advanced search

Re: Review Request: Proposal Ternary Right Fold Expression (P1012)

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 10 Jun 2020 10:24:31 -0400
On Wed, Jun 10, 2020 at 2:06 AM Dvir Yitzchaki via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> you can get a similar behavior with operator||
>
> ```
> template <std::size_t... is>
> T test_impl(std::size_t j, std::index_sequence<is...>)
> {
> T res;
> (void)( (j == is ? (res = f<is>(), true) : false) || ... || (throw
> std::range_error("Out of range"), false) );
> return res;
> }
> ```
> https://gcc.godbolt.org/z/8mJ96z
>

Unfortunately that requires `T` to be default-constructible and support
move-assignment, neither of which is necessarily true in modern code.
(Someone might even want to make `test_impl` return a *reference*.) Worse,
it requires all the `f<is>()` to have the same return type.
However, I agree, this should be mentioned in the paper, in a "Tony Table":
"here's how you can kinda do it in C++20, versus how I want to be able to
do it."
It would be useful to link to Jonathan Müller's "Nifty Fold Expression
Tricks" https://foonathan.net/2020/05/fold-tricks/#content as well. He
gives this specific trick under the heading "Find the first element that
matches the predicate."

–Arthur

Received on 2020-06-10 09:27:50