C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::ranges::equal(v, {1,2,3,4,5})

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Tue, 24 Jan 2023 18:48:45 +0000
On 24 January 2023 16:59:25 GMT, Arthur O'Dwyer via Std-Proposals <std-proposals_at_[hidden]> wrote:
>Hi Giuseppe, std-proposals,
>
>https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2248r7.html
>I just noticed today that P2248 "Enabling list-initialization for
>algorithms" still fails to enable this very common use-case of mine:
> std::vector<int> v = {1,2,3,4,5};
> assert(std::ranges::equal(v, {1,2,3,4,5}));
>Was this possibility ever discussed before?
>This *could* be handled by defaulting the `R2` parameter to
>`initializer_list`, but it probably makes more sense to add a whole new
>overload of `std::equal`, as follows. (This means it's quite reasonable
>that P2248 wouldn't have proposed this idea... but I'm still wondering if
>it's been discussed before.)
>
>// the existing overload
>template<ranges::input_range
><http://en.cppreference.com/w/cpp/ranges/input_range> R1, ranges::
>input_range <http://en.cppreference.com/w/cpp/ranges/input_range> R2,
>
> class Pred = ranges::equal_to
><http://en.cppreference.com/w/cpp/utility/functional/ranges/equal_to>,
> class Proj1 = std::identity
><http://en.cppreference.com/w/cpp/utility/functional/identity>, class Proj2
>= std::identity
><http://en.cppreference.com/w/cpp/utility/functional/identity>>
> requires std::indirectly_comparable
><http://en.cppreference.com/w/cpp/iterator/indirectly_comparable><ranges::
>iterator_t <http://en.cppreference.com/w/cpp/ranges/iterator_t><R1>,
>ranges::iterator_t <http://en.cppreference.com/w/cpp/ranges/iterator_t><R2>,
> Pred, Proj1, Proj2>
>constexpr bool equal( R1&& r1, R2&& r2, Pred pred = {},
> Proj1 proj1 = {}, Proj2 proj2 = {} );
>
>// the new overload
>template<ranges::input_range
><http://en.cppreference.com/w/cpp/ranges/input_range> R1, *class V2,*
>
> class Pred = ranges::equal_to
><http://en.cppreference.com/w/cpp/utility/functional/ranges/equal_to>,
> class Proj1 = std::identity
><http://en.cppreference.com/w/cpp/utility/functional/identity>, class Proj2
>= std::identity
><http://en.cppreference.com/w/cpp/utility/functional/identity>>
> requires std::indirectly_comparable
><http://en.cppreference.com/w/cpp/iterator/indirectly_comparable><ranges::
>iterator_t <http://en.cppreference.com/w/cpp/ranges/iterator_t><R1>, *const
>V2**,
> Pred, Proj1, Proj2>
>constexpr bool equal( R1&& r1, *std::initializer_list<V2>* r2, Pred pred = {
>},
> Proj1 proj1 = {}, Proj2 proj2 = {} );
>
>–Arthur

You probably want to enable initializer-list in the first argument as well, for symmetry. Also in both arguments, for consistency. These are three new overloads, including yours. A default template param for both arguments might be cleaner. Does it make a difference?

I suppose some other range algorithms could have this treatment. P2248 seem to focus on "value" arguments so far, where value_type can be inferred from an other range argument.

Cheers,
Lénárd

Received on 2023-01-24 18:48:53