On Friday, April 11, 2025, Lénárd Szolnoki <cpp@lenardszolnoki.com> wrote:
Can't work safely in general. Just because an id is used last lexically within a function it does not mean that it is the last used reference to the object. A previous call could leak the reference and a later call can use it.
Similarly, implicitly moved strings can silently invalidate string_views.
Yeah but isn't that kind of like saying that we shouldn't have 'std::string::c_str' because the pointers might become invalid if you do something with the string?
If you're going to use a string_view to refer to the contents of an std::string, then you already need to make sure that the string doesn't change too much. So you wouldn't use a trigraph '&&&' reference to refer to a string if either:
a) You've already stored the value of c_str()
b) You're going to plunder the string's resources