Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 564
Trass3r Avatar asked May 26 '19 19:05

Trass3r


1 Answers

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…

like image 133
Michael Kenzel Avatar answered Nov 11 '22 13:11

Michael Kenzel