C++ Logo

std-proposals

Advanced search

Re: Add first and last to the std::string_view

From: Nicholas Schwab <cpp_std_at_[hidden]>
Date: Fri, 22 Jan 2021 12:27:11 +0100
Hi,

for your specific example, the in my opinion best way would be

static_assert("hello world"sv.starts_with("hello"));

which is possible since C++20. But in general I agree, that it would be
nice to have these member functions. Specifically, I dislike calls to
string_view::substr with one argument (which is equivalent to your
proposed string_view::last):

"hello_world".substr(5);

This returns "_world"sv, but I do not find that obvious from the name
only. Both "hello"sv and "_"sv/'_' seem equally expectable results to me.

Regards,
Nicholas

On 22/01/2021 08:03, 康桓瑋 via Std-Proposals wrote:
> C++20 introduced the /std::span/ which makes it easier to deal
> with continuous rage. Especially the subview facilities: /first/, /last/
> and /subspan/. I wonder if we can add /first/ and /last/ function to
> /std::string_view/, then we can generate the /substr/ more intuitively
> and less fallible:
>
> static_assert("hello world"sv.first(5) == "hello");
>
>
> Thanks for your reply!
>

Received on 2021-01-22 05:27:14