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.