|
|
Subscribe / Log in / New account

The underlying std::string is freed?

The underlying std::string is freed?

Posted Jul 7, 2024 13:31 UTC (Sun) by Sesse (subscriber, #53779)
In reply to: The underlying std::string is freed? by NYKevin
Parent article: New features in C++26

My slightly different two cents: Returning const string& is a micro-optimization, and you should return _string_ everywhere. Not string_view. You have zero idea of the lifetime of a non-owning return parameter (e.g. what happens if someone else on a different thread starts accessing the object, does that invalidate the object you are holding?), and people are notoriously bad at documenting them.


to post comments

The underlying std::string is freed?

Posted Jul 9, 2024 17:55 UTC (Tue) by NYKevin (subscriber, #129325) [Link]

Yes, this is reasonable if you don't have a well-documented lifetime for your borrows. But if your function is internString, then returning a string defeats the purpose.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds