
Giovanni Piero Deretta wrote:
To: boost@lists.boost.org Sent: Tuesday, December 16, 2008 7:19:52 PM Subject: Re: [boost] Proposed templated integer_sort
On Tue, Dec 16, 2008 at 3:03 PM, Pyry Jahkola wrote:
Gevorg Voskanyan wrote:
I suggest modifying CastFloatIter() function to use memcpy for accessing floats as integers, as shown below:
Hmm, I don't think it's any better than reinterpret casting.
Yes it is. It has the nice property of not leading to UB. And the result is well defined (the bitwise representation of the source is copied into the destination).
(It actually does the reinterpret cast when converting float const * to void const *...)
Not really.
I'd suggest using a union instead, for the purpose of treating a float as integer:
template inline To bitwise_cast(From const & from) { BOOST_STATIC_ASSERT(sizeof(To) == sizeof(From)); union { To to; From from; } u; u.from = from; return u.to; }
This is also undefined behavior even if it is explicitly supported by most compilers as an extension. It is not necessarily better than a memcpy and might actually be slower.
-- gpd
Thanks Giovanni, I could not reply any better. Best Regards, Gevorg