C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Ranges algorithms should prioritize container defined algorithms

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Wed, 16 Oct 2024 09:52:37 +0200
Il 16/10/24 03:04, Anton Petushkov via Std-Proposals ha scritto:
> Another example would be if you're passing a string into ranges::find(),
> you'd likely want the algorithm to use the find() method in the string
> class rather than the naive std::ranges implementation.

As much as I sympathize with this goal, there's a ton of small
incompatibilities to iron out. The string algorithms you're mentioning
are subtly incompatible with the ranges counterparts:


constexpr std::string_view s = "hello";

static_assert(s.starts_with("he")); // OK
static_assert(std::ranges::starts_with(s, "he")); // ERROR¹


This is by specification, which means having ranges::starts_with
"forward" to string::starts_with would yield the wrong result.



¹ If you're wondering what's going on, the algorithm sees the second
range as a const char[3] range, which *includes* the NUL terminator.


My 2 c,
--
Giuseppe D'Angelo

Received on 2024-10-16 07:52:41