Boost logo

Boost :

Subject: Re: [boost] [optional] get() misses optional r-value overload in contrast to operator* and value()
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2018-03-23 15:43:42


2018-03-23 16:32 GMT+01:00 Viktor Sehr <viktor.sehr_at_[hidden]>:

> Hi Andrzej, thanks for your response.
>
> I do not get the same result as you using latest visual studio (as for
> today 15.6.4 with std=latest), I'm not sure on what would be the correct
> behavior but to me it seems erroneous that the unique_ptr has lost it's
> value. Note that using std::optional yields the same result as below.
>
> auto func0() {
> return boost::make_optional(std::make_unique<int>(42));
> }
> const auto& x = func0().value();
> assert(x); // true
> assert(*x = 42); // true
>

I am convinced that you are hitting an UB. Temporary returned by func0() is
destroyed in the first line. After that the reference is dangling. One of
the consequences of UB may be the program doing exactly what you expect in
your compiler/platform/version.

>
> I agree that declaring x as a reference rather than a value is sketchy,
> this is just how I happened to find the bug, I suppose there are other
> cases which might be errorous.
> While on the subject I think that boost::optional should add the member
> function has_value() in addition to is_initialized() to make it
> syntactically compatible with std::optional.
>

Thanks. Recorded: https://github.com/boostorg/optional/issues/52

Regards,
&rzej;


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