explicit_conversion.hpp 3.4 KB

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