This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
strict aliasing violation
- From: Piotr Wyderski <piotr dot wyderski at gmail dot com>
- To: gcc at gcc dot gnu dot org
- Date: Mon, 25 Jan 2010 13:24:36 +0100
- Subject: strict aliasing violation
I have a hash function hash(T v) overloaded for
all integral types. I want to provide a variant for
float and double, which should work as follows:
take the floating-point value, treat its binary
representation as uint32_t/uint64_t and use
the result as a parameter for an integral hash().
However, GCC warns me about strict aliasing
rules violation, which is technically correct, but
in this case is intended. How do I perform this
conversion ina GCC-friendly way? Even that
produces a warning:
inline hash_type hash(float v) {
return hash(*reinterpret_cast<const
std::uint32_t*>(reinterpret_cast<const char*>(&v)));
}
but I expected char* to be allowed to alias anything.
Best regards
Piotr Wyderski