C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Add Projection Support for std::ranges::views

From: Barry Revzin <barry.revzin_at_[hidden]>
Date: Fri, 15 Sep 2023 11:32:36 -0500
On Fri, Sep 15, 2023, 11:16 AM Jonathan Wakely via Std-Proposals <
std-proposals_at_[hidden]> wrote:

>
>
> On Thu, 14 Sept 2023 at 16:06, Grzegorz Sikorski via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Thanks all for your answers. I obviously misread the description in
>> https://en.cppreference.com/w/cpp/ranges/filter_view (in other places
>> the overload for a projection is explicitly shown). Yes, this helps a lot
>> and solves one problem. However the original issue I was trying to fix
>> recently was this: https://godbolt.org/z/TW8P5xqxz. I use range-v3 as a
>> polyfill for the missing std::views::chunk_by, maybe this is the problem.
>>
>
> Firstly, std::views::chunk_by is not missing, it's part of C++23 so
> available if you use -std=c++23
>
>
> Secondly, your attempts to use it are incorrect, that's why it doesn't
> work. The argument needs to be a binary predicate, so neither of these is
> correct:
>
> // Neither of below work:
> // auto active_buttons = buttons |
> ranges::views::chunk_by(&Button::enabled, std::equal_to<bool>{});
> // auto active_buttons = buttons |
> ranges::views::chunk_by(&Button::enabled);
>
> This works:
>
> auto active_buttons = buttons | std::views::chunk_by([](auto& l, auto&
> r){ return l.enabled == r.enabled; });
>
> The problem here is not related to lack of support for projections, it's
> that you need a binary predicate and &Button:enabled is not a binary
> predicate.
>

Or, if you want the more convenient syntax, we dont need every algorithm to
implement what is basically a function adapter:

views::chunk_by(boost::hof::proj(&Button::enabled, std::equal_to()))

prof(p, f)(x, y) does f(p(x), p(y))

Barry

Received on 2023-09-15 16:32:50