C++ Logo

std-proposals

Advanced search

Re: why does std::basic_string should accept an std::basic_string_view with the same charT and Traits

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 14 Sep 2020 17:20:05 -0400
On Mon, Sep 14, 2020 at 2:23 PM Thiago Macieira via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Monday, 14 September 2020 06:56:51 PDT Jason McKesson via Std-Proposals
> wrote:
> > But yes, I do agree that saying "is like comparing unsigned with float
> > point or bool with int" is wrong. They are *conceptually* storing the
> > same data; they are only different types because of the need to do
> > special static things with their allocators. The allocator type being
> > part of the string's type is a wart of the language, not a fundamental
> > part of what it means to be a "string". So long as the character and
> > traits types are the same, I think it is perfectly valid to compare
> > them.
>
> The way it is defined to be, it actually is. The allocator defines how to
> refer to the data itself, including what a pointer or reference to the data
> itself is. String comparison calls the character traits compare() function
> with the pointer to the data on both strings and the length, both things
> requiring the allocator to define.

[char.traits.require] lays down the specific requirements for a type
to conform to in order to be a viable character traits class. It
specifically deals in language pointers to `char_type`, not to a
pointer-like object or a type with `pointer_traits` or whatever. It
deals in `char_type*`s.

So `char_traits::compare` is based on language pointers, not
`allocator_traits::pointer`. In fact, `c_str` returns a `CharT
const*`. Given that, you can compare two strings with different
allocators but the same `CharT` and `CharTraits` types easily.

So I don't buy the notion that, even if the allocators use different
`pointer` types, that the two strings they point to cannot be
compared.

Basically, if it's possible to compare both by converting them to
string_views, then it's ought to be equally possible to compare them
by comparing them directly. A string_view of a string is logically
equivalent to the string itself, so non-modifying operations on views
ought to be valid on the strings themselves.

Received on 2020-09-14 16:20:17