C++ Logo


Advanced search

Subject: Re: [std-proposals] Add first and last to the std::string_view
From: Arthur O'Dwyer (arthur.j.odwyer_at_[hidden])
Date: 2021-01-22 07:30:54

On Fri, Jan 22, 2021 at 6:27 AM Nicholas Schwab via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> 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.

Absolutely. However, notice that `.substr(5)` isn't `.last(5)`; it's
The vocabulary terms for "first" and "last" here, since Ranges-v3, are
actually "take" and "drop":
    assert("hello_world"sv.take(5) == "hello");
    assert("hello_world"sv.drop(5) == "_world");
    assert("hello_world"sv.drop_all_but(5) == "world");

The standard library was "poisoned" early on with APIs (like
single-argument .substr(5)) that take integers to indicate either "start
here" or "go until here" with no explanation. For example, it was recently
brought to my attention that

    // https://godbolt.org/z/69xY4x
    assert(std::string("hello_world", 5) == "hello");
    assert(std::string("hello_world"s, 5) == "_world");


STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups