is_restricted_conversion.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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_IS_RESTRICTED_CONVERSION_HPP
  7. #define BOOST_MP_IS_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. static constexpr bool category_conditional_is_true =
  15. ( (static_cast<boost::multiprecision::number_category_type>(number_category<From>::value) == number_kind_floating_point)
  16. && (static_cast<boost::multiprecision::number_category_type>(number_category<To >::value) == number_kind_integer))
  17. || ( (static_cast<boost::multiprecision::number_category_type>(number_category<From>::value) == number_kind_rational)
  18. && (static_cast<boost::multiprecision::number_category_type>(number_category<To >::value) == number_kind_integer))
  19. || ( (static_cast<boost::multiprecision::number_category_type>(number_category<From>::value) == number_kind_fixed_point)
  20. && (static_cast<boost::multiprecision::number_category_type>(number_category<To >::value) == number_kind_integer))
  21. || (static_cast<boost::multiprecision::number_category_type>(number_category<From>::value) == number_kind_unknown)
  22. || (static_cast<boost::multiprecision::number_category_type>(number_category<To >::value) == number_kind_unknown);
  23. using type = typename std::conditional<category_conditional_is_true,
  24. std::integral_constant<bool, true>,
  25. std::integral_constant<bool, false>>::type;
  26. static constexpr bool value = type::value;
  27. };
  28. template <typename From, typename To>
  29. struct is_restricted_conversion
  30. {
  31. using type = typename std::conditional<
  32. ((is_explicitly_convertible<From, To>::value && !std::is_convertible<From, To>::value) || is_lossy_conversion<From, To>::value),
  33. std::integral_constant<bool, true>,
  34. std::integral_constant<bool, false>>::type;
  35. static constexpr const bool value = type::value;
  36. };
  37. }}} // namespace boost::multiprecision::detail
  38. #endif // BOOST_MP_IS_RESTRICTED_CONVERSION_HPP