KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to check that a given double/float variable has the actual bit pattern 0x0. Don't ask why, it's used in a function in Qt ( qIsNull() ) that I'd like to be constexpr . The original code used a union: union { double d; int64_t i; } u; u.d = d; return u.i == 0; This doesn't work as a constexpr of course. The next try was with reinterpret_cast : return *reinterpret_cast<int64_t*>(&d) == 0; But while that works as a constexpr in GCC 4.7, it fails (rightfully, b/c of 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; } }; // in qIsNull(): return Converter<double,int64_t>(d); But that's not clever enough for Clang, 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?
Tags (comma-separated)
Save Edits
Cancel