C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Addition of std::contains to Simplify Container Value Checks

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Sun, 24 Dec 2023 18:25:36 +0100
Il 24/12/23 14:50, Ville Voutilainen via Std-Proposals ha scritto:
> On Sun, 24 Dec 2023 at 15:44, Bjorn Reese via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>>
>> On 12/23/23 10:20, Robert Sitton via Std-Proposals wrote:
>>
>>> Function Signature:
>>> template <typename Container, typename T>
>>> bool std::contains(const Container& container, const T& value);
>>
>> Why does it deviate from all other standard algorithms that takes
>> iterators (or ranges for std::ranges algorithms)?
>
> Because it doesn't operate on iterators. If you pass a std::map to it,
> it'll call std::map::contains,
> it doesn't perform a silly linear walk from one iterator to another.
> What we're looking at here is basically
> "call the argument's contains() if available, do a ranges::contains otherwise".

Indeed; see also std::erase/erase_if, although for them there isn't even
a way to do the "otherwise" part -- the container must offer its own
overload, which effectively makes them customization points.

My 2 c,
-- 
Giuseppe D'Angelo

Received on 2023-12-24 17:25:40