Trass3r
Trass3r

Reputation: 6287

reinterpret_cast from rvalue to rvalue reference

Just a simple does it compile test. gcc accepts the following while clang and msvc reject it: https://godbolt.org/z/DlUasL

float test()
{
    return reinterpret_cast<float&&>(0x7F800000);
}

Which one is right according to the standard?

Upvotes: 10

Views: 1162

Answers (1)

Michael Kenzel
Michael Kenzel

Reputation: 15941

The conversion this reinterpret_cast expression seeks to perform is not among the list of conversions [expr.reinterpret.cast] that a reinterpret_cast can perform [expr.reinterpret.cast]/1. 0x7F800000 is a literal of integral type. The only conversion reinterpret_cast could perform that converts from a value of integral type to some other type is that of turning such a value into a pointer type [expr.reinterpret.cast]/5. float&& is a reference type, not a pointer type. The only conversion reinterpret_cast can perform that converts to a reference type is that of converting a glvalue expression [expr.reinterpret.cast]/11. 0x7F800000 is not a glvalue. Thus, this code is ill-formed. The fact that GCC would accept this is quite surprising to me and, I would say, definitely a bug that should be reported…

Upvotes: 9

Related Questions