Boost logo

Boost :

Subject: [boost] float_mem_cast
From: Steven Ross (spreadsort_at_[hidden])
Date: 2009-05-26 09:46:33


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;
  }


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