explicit_conversion.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_EXPLICIT_CONVERTIBLE_HPP
  7. #define BOOST_MP_EXPLICIT_CONVERTIBLE_HPP
  8. #include <boost/config.hpp>
  9. #include <boost/multiprecision/detail/number_base.hpp> // number_category
  10. namespace boost {
  11. namespace multiprecision {
  12. namespace detail {
  13. template <unsigned int N>
  14. struct dummy_size
  15. {};
  16. template <typename S, typename T>
  17. struct has_generic_interconversion
  18. {
  19. using type = typename std::conditional<
  20. is_number<S>::value && is_number<T>::value,
  21. typename std::conditional<
  22. number_category<S>::value == number_kind_integer,
  23. typename std::conditional<
  24. number_category<T>::value == number_kind_integer || number_category<T>::value == number_kind_floating_point || number_category<T>::value == number_kind_rational || number_category<T>::value == number_kind_fixed_point,
  25. std::integral_constant<bool, true>,
  26. std::integral_constant<bool, false> >::type,
  27. typename std::conditional<
  28. number_category<S>::value == number_kind_rational,
  29. typename std::conditional<
  30. number_category<T>::value == number_kind_rational || number_category<T>::value == number_kind_rational,
  31. std::integral_constant<bool, true>,
  32. std::integral_constant<bool, false> >::type,
  33. typename std::conditional<
  34. number_category<T>::value == number_kind_floating_point,
  35. std::integral_constant<bool, true>,
  36. std::integral_constant<bool, false> >::type>::type>::type,
  37. std::integral_constant<bool, false> >::type;
  38. };
  39. template <typename S, typename T>
  40. struct is_explicitly_convertible_imp
  41. {
  42. template <typename S1, typename T1>
  43. static int selector(dummy_size<sizeof(new T1(std::declval<
  44. S1
  45. >()))>*);
  46. template <typename S1, typename T1>
  47. static char selector(...);
  48. static constexpr const bool value = sizeof(selector<S, T>(0)) == sizeof(int);
  49. using type = boost::integral_constant<bool, value>;
  50. };
  51. template <typename From, typename To>
  52. struct is_explicitly_convertible : public is_explicitly_convertible_imp<From, To>::type
  53. {
  54. };
  55. }}} // namespace boost::multiprecision::detail
  56. #endif