is_restricted_conversion.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright Vicente J. Botet Escriba 2009-2011
  3. // Copyright 2012 John Maddock. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MP_RESTRICTED_CONVERSION_HPP
  7. #define BOOST_MP_RESTRICTED_CONVERSION_HPP
  8. #include <boost/multiprecision/traits/explicit_conversion.hpp>
  9. #include <boost/multiprecision/detail/number_base.hpp>
  10. namespace boost { namespace multiprecision { namespace detail {
  11. template <class From, class To>
  12. struct is_lossy_conversion
  13. {
  14. using type = typename std::conditional<
  15. ((number_category<From>::value == number_kind_floating_point) && (number_category<To>::value == number_kind_integer))
  16. /* || ((number_category<From>::value == number_kind_floating_point) && (number_category<To>::value == number_kind_rational))*/
  17. || ((number_category<From>::value == number_kind_rational) && (number_category<To>::value == number_kind_integer)) || ((number_category<From>::value == number_kind_fixed_point) && (number_category<To>::value == number_kind_integer)) || (number_category<From>::value == number_kind_unknown) || (number_category<To>::value == number_kind_unknown),
  18. std::integral_constant<bool, true>,
  19. std::integral_constant<bool, false>>::type;
  20. static constexpr const bool value = type::value;
  21. };
  22. template <typename From, typename To>
  23. struct is_restricted_conversion
  24. {
  25. using type = typename std::conditional<
  26. ((is_explicitly_convertible<From, To>::value && !std::is_convertible<From, To>::value) || is_lossy_conversion<From, To>::value),
  27. std::integral_constant<bool, true>,
  28. std::integral_constant<bool, false>>::type;
  29. static constexpr const bool value = type::value;
  30. };
  31. }}} // namespace boost::multiprecision::detail
  32. #endif // BOOST_MP_RESTRICTED_CONVERSION_HPP