The underlying std::string is freed?
The underlying std::string is freed?
Posted Jul 6, 2024 14:15 UTC (Sat) by intelfx (subscriber, #130118)In reply to: The underlying std::string is freed? by Cyberax
Parent article: New features in C++26
Yes, but there was an exception: binding to a reference in a returned value did not extend the lifetime of the temporary.
In C++23 and earlier, N4950 6.7.7 [class.temporary]/6.11 stated that returned values are excluded from the lifetime-extension rule.
> The lifetime of a temporary bound to the returned value in a function return statement ([stmt.return]) is not extended; the temporary is destroyed at the end of the full-expression in the return statement.
This wording was included in the standard up to N4971.
The proposal we are discussing here (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/...) removes that exception and instead makes such references ill-formed.
So basically the previous rule was "well-formed, but return values do not extend lifetimes, so you get a dangling reference" and it was replaced with a rule that such references are now ill-formed.