+23
−4
+20
−0
+95
−0
Loading
operator*() && / const&& and value() && / const&& now return T by
value (moved, or copied for const&&) instead of a reference into the
optional. With the reference return, a range-for or a const reference
bound to the result of a function returning an optional dangles in C++20:
for (auto x : *lookup()) // lookup() returns optional<vector>
const auto &r = *lookup();
The range/reference binds into the temporary optional, which is destroyed
at the end of the full expression (only C++23's P2718 extends temporaries
in the range initializer). A prvalue return makes the language extend its
lifetime instead; the cost is one move. Types that can't be moved (or, for
const&&, copied) keep the reference return.
Found in mammut, where an index lookup loop read freed rowids.
New e2e test cxx_optional_rvalue_test.
Co-Authored-By:
Claude Opus 5.5 <noreply@anthropic.com>