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: Nikolay Mihaylov <nmmm_at_[hidden]>
Date: Sun, 13 Sep 2020 22:21:47 +0300
I disagree, but will leave the exercise for you.

Just will remind you that std::string is *guaranteed* null terminated.
Sure you can do strings like "hello\0world\0" but these are more abuse than
something that is useful.
Also they won't work in C too.

const char *a = "hello\0world";
const char *b = "hello";

Does *a* is equal to *b*? It depends what you meant.
This is why my StringRef implementation uses memcmp rather than strcmp.

On Sun, Sep 13, 2020 at 9:36 PM Thiago Macieira <thiago_at_[hidden]> wrote:

> On Sunday, 13 September 2020 11:08:17 PDT Nikolay Mihaylov wrote:
> > however, the compiler knows what strcmp is , so if you do
> > strcmp(a.c_str(), b.c_str()) == 0;
>
> No, you can't. That's not the same as the *correct* memcmp.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel DPG Cloud Engineering
>
>
>
>

Received on 2020-09-13 14:22:25