Boost logo

Boost :

Subject: Re: [boost] float_mem_cast
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-05-26 09:53:32


AMDG

Steven Ross wrote:
> There is one function necessary for users to be able to cast their own float
> types. Should it be in spreadsort.hpp or some separate header file? It
> seems silly to have a separate header file for it, but I guess it might be
> used on its own.
>
> //Casts a float to the specified integer type
> template<class Data_type, class Cast_type>
> inline Cast_type
> float_mem_cast(const Data_type & data)
> {
> //Only cast IEEE floating-point numbers, and only to a same-sized
> integer
> BOOST_STATIC_ASSERT(sizeof(Cast_type) == sizeof(Data_type));
> BOOST_STATIC_ASSERT(std::numeric_limits<Data_type>::is_iec559);
> BOOST_STATIC_ASSERT(std::numeric_limits<Cast_type>::is_integer);
> Cast_type result;
> std::memcpy(&result, &data, sizeof(Cast_type));
> return result;
> }
>

Pu it in a detail namespace. If others need the same functionality,
we can discuss generalizing it then.

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk