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-23 22:20:23


Viktor Sehr wrote:

> Clang (6.0) emits "ud2" instructions for both auto&& and const auto&. If
> the const auto& can bind directly to an r-value, this must mean Clang is
> wrong, or am I missing something?

"ud2" is Clang's way of encoding undefined behavior. This is an invalid
instruction that crashes. Both auto&& and auto const& do the same thing -
bind to the rvalue reference returned by value() &&.

> GCC on the other hand emits code for both functions, although with slight
> difference (I'm not skilled enough in assembler to analyze the GCC output
> in further detail)
>
> Code example: https://godbolt.org/g/7fjszE <https://godbolt.org/g/uNJoZB>

GCC crashes too, in another creative way. It returns *p, where the pointer p
is [rsp+8], which is NULL. If you change the functions to

int * func1() {
    const auto& x = func0().value();
    return &*x;
}

you'll see that Clang returns NULL and GCC returns the value in [rsp+8]
which is also NULL.

So both compilers deliberately dereference a NULL pointer because they see
that the reference `x` is no longer valid.


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