C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Forbid optimisations on std::basic_string implementations.

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Mon, 18 Aug 2025 14:59:08 +0200
For this use case you could use a unique_ptr or shared_ptr to the string as underlying object.   If you take a string_view from it and then move either of the smart pointers, the string_view continues to be valid.   -----Ursprüngliche Nachricht----- Von:Simon Schröder via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mo 18.08.2025 14:52 Betreff:Re: [std-proposals] Forbid optimisations on std::basic_string implementations. An:Henry Miller via Std-Proposals <std-proposals_at_[hidden]>; CC:Simon Schröder <dr.simon.schroeder_at_[hidden]>; On 18. Aug 2025, at 13:56, Bo Persson via Std-Proposals <std-proposals_at_[hidden]> wrote: > > On 2025-08-18 at 13:18, Andrey Semashev via Std-Proposals wrote: >> On 18 Aug 2025 13:33, Ben Crowhurst via Std-Proposals wrote: >>> >>> There is a history of STL implementations introducing string >>> optimisations, for example, copy-on-write in GCC (<=4), adoption of >>> small string optimisation (SSO) - with varying details across GCC, >>> Clang, and MSVC. >>> >>> Focusing on SSO, using std::string, along with std::string_view and move >>> semantics, introduces a frustrating folly. >> Why? What is the problem with SSO? > > If you get a string_view to a string, and then move the string, the string_view may, or may not, move along - all because of SSO. If you have a string_view or a span to a moved from object there is no reason to believe that the view/span is still valid. It is at least implementation defined behavior if not undefined behavior. If the view still works after a move you just got lucky. It is quite similar for iterators to be invalid for some containers after certain operations on the container. This is certainly not a reason to disallow SSO. If you need a string+string_view with a specific behavior you should implement your own. (Hopefully compatible/convertible to a std::string.) > > I too prefer the performance of small strings over fixing this. > > -- > Std-Proposals mailing list > Std-Proposals_at_[hidden] > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-08-18 13:09:49