No, it doesn't, your code has undefined behavior when you try to print the value of paw
.
The C++ rules for lifetime extension only apply when a temporary object is bound to an rvalue reference or lvalue reference to const
, but realCat
is not a temporary and binding it to an optional
reference doesn't extend it's lifetime.
In any case, these lifetime extension rules don't apply to boost::optional<T&>
. The documentation for boost::optional
mentions this:
Rvalue references and lvalue references to const have the ability in C++ to extend the life time of a temporary they bind to. Optional references do not have this capability ...
As for std::optional
, your program would be ill-formed if you tried to create std::optional<T&>
.
[optional.syn]/1
A program that necessitates the instantiation of template optional
for a reference type, or for possibly cv-qualified types in_place_t
or nullopt_t
is ill-formed.