cast.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // (c) Copyright Fernando Luis Cacciola Carballal 2000-2004
  2. // Use, modification, and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See library home page at http://www.boost.org/libs/numeric/conversion
  6. //
  7. // Contact the author at: fernando_cacciola@hotmail.com
  8. //
  9. //
  10. // Revision History
  11. //
  12. // 19 Nov 2001 Syntatic changes as suggested by Darin Adler (Fernando Cacciola)
  13. // 08 Nov 2001 Fixes to accommodate MSVC (Fernando Cacciola)
  14. // 04 Nov 2001 Fixes to accommodate gcc2.92 (Fernando Cacciola)
  15. // 30 Oct 2001 Some fixes suggested by Daryle Walker (Fernando Cacciola)
  16. // 25 Oct 2001 Initial boostification (Fernando Cacciola)
  17. // 23 Jan 2004 Inital add to cvs (post review)s
  18. // 22 Jun 2011 Added support for specializing cast policies via numeric_cast_traits (Brandon Kohn).
  19. //
  20. #ifndef BOOST_NUMERIC_CONVERSION_CAST_25OCT2001_HPP
  21. #define BOOST_NUMERIC_CONVERSION_CAST_25OCT2001_HPP
  22. #include <boost/detail/workaround.hpp>
  23. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  24. # include<boost/numeric/conversion/detail/old_numeric_cast.hpp>
  25. #else
  26. #include <boost/type.hpp>
  27. #include <boost/numeric/conversion/converter.hpp>
  28. #include <boost/numeric/conversion/numeric_cast_traits.hpp>
  29. namespace boost
  30. {
  31. template <typename Target, typename Source>
  32. inline Target numeric_cast( Source arg )
  33. {
  34. typedef numeric::conversion_traits<Target, Source> conv_traits;
  35. typedef numeric::numeric_cast_traits<Target, Source> cast_traits;
  36. typedef boost::numeric::converter
  37. <
  38. Target,
  39. Source,
  40. conv_traits,
  41. typename cast_traits::overflow_policy,
  42. typename cast_traits::rounding_policy,
  43. boost::numeric::raw_converter< conv_traits >,
  44. typename cast_traits::range_checking_policy
  45. > converter;
  46. return converter::convert(arg);
  47. }
  48. using numeric::bad_numeric_cast;
  49. } // namespace boost
  50. #endif
  51. #endif