C++ Logo

std-proposals

Advanced search

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

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Wed, 25 Jan 2023 03:07:17 +0100
Hello,

On 24/01/2023 17:59, Arthur O'Dwyer 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.)

In the context of P2248, the answer would be no, it has not been
proposed / it's out of scope. P2248's design is to default value-like
arguments (e.g. the "value" of std::find) of algorithms using the value
type of the range(s) passed as inputs.

What you propose certainly makes lots of sense, we already have some
algorithm (e.g. std::(ranges)::max) that supports initializer lists
through a dedicated overload, not dissimilar to the code you pasted. One
can imagine more examples of same nature (algorithms), as well as extend
this to range functionality (e.g. views::zip, views::join_with_view --
etc.) where one would like to simply pass `{a, b, c}`.

It's a bit unfortunate that braced lists are still a weird datatype.
They don't deduce template type parameters, they're not views / viewable
ranges, etc.; one has to introduce additional overloads or add defaults.
I wonder if some codebase has created some tiny range class just to ease
the kind of calls that you want to streamline.

Thank you,

-- 
Giuseppe D'Angelo

Received on 2023-01-25 02:07:21