Boost logo

Boost :

Subject: Re: [boost] [optional] get() misses optional r-value overload in contrast to operator* and value()
From: Peter Dimov (lists_at_[hidden])
Date: 2018-03-25 00:50:06


Viktor Sehr wrote:

> Thanks for the answer, but just so I understand, you said a
> const-reference binds directly to a r-value as when passing an r-value to
> a const reference argument. Doesn't this mean that both Clang and GCC
> compiles this erroneously?

No, both are correct; optional::value()&& returns an rvalue reference to the
value inside the optional, to which the auto&& or auto const& binds. When
the optional temporary is destroyed, this reference becomes dangling.

> auto temp_uptr = std::move(temp_optional.value());

There is no temp_uptr. For there to have been, value()&& would have needed
to return T, not T&&. But it doesn't.

It's roughly

template<class T> class optional
{
    T t_;

publlic:

    T&& value()&& { return std::move(t_); }
};

so in

auto temp_optional = boost::make_optional(std::make_unique<int>(42));
const auto& x = temp_optional.value();

x binds to temp_optional.t_.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk