explicit_conversion.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/type_traits/is_convertible.hpp>
  9. #include <boost/utility/declval.hpp>
  10. namespace boost {
  11. namespace multiprecision {
  12. namespace detail {
  13. template <int N>
  14. struct dummy_size {};
  15. template<typename S, typename T>
  16. struct has_generic_interconversion
  17. {
  18. typedef typename mpl::if_c <
  19. is_number<S>::value && is_number<T>::value,
  20. typename mpl::if_c <
  21. number_category<S>::value == number_kind_integer,
  22. typename mpl::if_c<
  23. number_category<T>::value == number_kind_integer
  24. || number_category<T>::value == number_kind_floating_point
  25. || number_category<T>::value == number_kind_rational
  26. || number_category<T>::value == number_kind_fixed_point,
  27. mpl::true_,
  28. mpl::false_
  29. >::type,
  30. typename mpl::if_c<
  31. number_category<S>::value == number_kind_rational,
  32. typename mpl::if_c<
  33. number_category<T>::value == number_kind_rational
  34. || number_category<T>::value == number_kind_rational,
  35. mpl::true_,
  36. mpl::false_
  37. >::type,
  38. typename mpl::if_c<
  39. number_category<T>::value == number_kind_floating_point,
  40. mpl::true_,
  41. mpl::false_
  42. >::type
  43. >::type
  44. > ::type,
  45. mpl::false_
  46. > ::type type;
  47. };
  48. template<typename S, typename T>
  49. struct is_explicitly_convertible_imp
  50. {
  51. #ifndef BOOST_NO_SFINAE_EXPR
  52. template<typename S1, typename T1>
  53. static type_traits::yes_type selector(dummy_size<sizeof(static_cast<T1>(declval<S1>()))>*);
  54. template<typename S1, typename T1>
  55. static type_traits::no_type selector(...);
  56. static const bool value = sizeof(selector<S, T>(0)) == sizeof(type_traits::yes_type);
  57. typedef boost::integral_constant<bool, value> type;
  58. #else
  59. typedef typename has_generic_interconversion<S, T>::type gen_type;
  60. typedef mpl::bool_<boost::is_convertible<S, T>::value || gen_type::value> type;
  61. #endif
  62. };
  63. template<typename From, typename To>
  64. struct is_explicitly_convertible : public is_explicitly_convertible_imp<From, To>::type
  65. {
  66. };
  67. #ifdef BOOST_NO_SFINAE_EXPR
  68. template<class Backend1, expression_template_option ExpressionTemplates1, class Backend2, expression_template_option ExpressionTemplates2>
  69. struct is_explicitly_convertible<number<Backend1, ExpressionTemplates1>, number<Backend2, ExpressionTemplates2> >
  70. : public is_explicitly_convertible<Backend1, Backend2>
  71. {
  72. };
  73. #endif
  74. }}} // namespaces
  75. #endif