I want to check that the given double / float variable has the actual bit pattern 0x0. Don't ask why, he used in the Qt ( qIsNull() ) function that I would like to be constexpr .
The source code uses the union:
union { double d; int64_t i; } u; ud = d; return ui == 0;
It does not work like constexpr , of course.
The following attempt was with reinterpret_cast :
return *reinterpret_cast<int64_t*>(&d) == 0;
But while this works like constexpr in GCC 4.7, it fails (by right, b / c pointer manipulation) in Clang 3.1.
The final idea was to go Alexandrescuesque and do this:
template <typename T1, typename T2> union Converter { T1 t1; T2 t2; explicit constexpr Converter( T1 t1 ) : t1(t1) {} constexpr operator T2() const { return t2; } };
But this is not too smart for the Clan, either:
note: read of member 't2' of union with active member 't1' is not allowed in a constant expression constexpr operator T2() const { return t2; } ^
Does anyone else have a good idea?
c ++ floating-point casting c ++ 11 constexpr
Marc Mutz - mmutz
source share