C++ Logo


Advanced search

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

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Wed, 13 Sep 2023 17:13:55 +0100
On Wed, 13 Sept 2023 at 16:20, Grzegorz Sikorski via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hello,
> I am new to the std-proposals and I assume something like this has already
> been considered in the past, but I wonder if it was rejected for any reason
> or is it planned to address at some point?
> I started to notice a pattern that appears in many places. Consider a code
> like this:
> struct Button {
> std::string name;
> bool enabled;
> };
> auto get_enabled(const auto& buttons) {
> return std::ranges::views::filter([](const auto& button) { return
> button.enabled; });
> }
> What I find useful is to add something like
> https://godbolt.org/z/7Y5bdeddd. Projection support could also be useful
> in some other std::ranges::views namespace members.

This already works, you can just use
std::ranges::views::filter(&Button::enabled) (or
std::views::filter(&Button::enabled) which means the same thing).

All ranges algorithms use std::invoke to call the projection, which means
they support pointers to members already. (This is probably the main reason
that ranges code compiles slowly).

Received on 2023-09-13 16:14:10