C++ Logo


Advanced search

Re: [std-proposals] Adding a .slice() method to std::basic_string

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 12 Mar 2024 13:40:57 -0700
On Tuesday, 12 March 2024 13:21:35 PDT Rhidian De Wit via Std-Proposals wrote:
> We can't mimic Python's exact syntax (to my knowledge), but I feel like
> this would be a great gain over the current *basic_string::substr() *since
> this new function *basic_string::slice()* would have a similar API to
> Python's string slicing; requiring a starting index and an ending index.

Please substantiate that claim. "I feel like" isn't enough.

The only difference appears to be that it specifies the end position, not the
length. Why is that better? And why is adding a method that differs to the
existing one only by a closely-related concept but using the exact same data
types not going to lead to even more confusion?

> The full function syntax would look something like:
> *string string::slice(size_t start, size_t end);*

Nitpick: "sliced" (verb in the past tense) because it returns a new object,
instead of "slice" (verb in the imperative) which would indicate it modifies
the current object. Yes, it also matches the noun "slice" and one could argue
that it returns a slice, but most other functions in basic_string that match a
verb in the imperative are mutators (clear, reserve, resize, shrink_to_fit,
append, assign, etc.)

Also matches QString::slice and QString::sliced. Which, unlike your proposal,
take lengths not positions for the end of the slice.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Principal Engineer - Intel DCAI Cloud Engineering

Received on 2024-03-12 20:40:59