explicit_conversion.hpp 3.6 KB

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