summaryrefslogtreecommitdiff
path: root/boost/multiprecision/traits/explicit_conversion.hpp
blob: b4cd8215e091f99b5d8d93a0009f1540ffd6d155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
///////////////////////////////////////////////////////////////////////////////
//  Copyright Vicente J. Botet Escriba 2009-2011
//  Copyright 2012 John Maddock. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_MP_EXPLICIT_CONVERTIBLE_HPP
#define BOOST_MP_EXPLICIT_CONVERTIBLE_HPP

#include <boost/type_traits/is_convertible.hpp>
#include <boost/utility/declval.hpp>


namespace boost{ namespace multiprecision{ namespace detail{

template <int N>
struct dummy_size{};

template<typename S, typename T>
struct has_generic_interconversion
{
   typedef typename mpl::if_c<
      is_number<S>::value && is_number<T>::value,
      typename mpl::if_c<
         number_category<S>::value == number_kind_integer,
         typename mpl::if_c<
            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,
            mpl::true_, 
            mpl::false_
         >::type,
         typename mpl::if_c<
            number_category<S>::value == number_kind_rational,
            typename mpl::if_c<
               number_category<T>::value == number_kind_rational
               || number_category<T>::value == number_kind_rational,
               mpl::true_,
               mpl::false_
            >::type,
            typename mpl::if_c<
               number_category<T>::value == number_kind_floating_point,
               mpl::true_,
               mpl::false_
            >::type
         >::type
      >::type,
      mpl::false_
   >::type type;
};

template<typename S, typename T>
struct is_explicitly_convertible_imp
{
#ifndef BOOST_NO_SFINAE_EXPR
   template<typename S1, typename T1>
   static type_traits::yes_type selector(dummy_size<sizeof(static_cast<T1>(declval<S1>()))>*);

   template<typename S1, typename T1>
   static type_traits::no_type selector(...);

   static const bool value = sizeof(selector<S,T>(0)) == sizeof(type_traits::yes_type);

   typedef boost::integral_constant<bool,value> type;
#else
   typedef typename has_generic_interconversion<S, T>::type gen_type;
   typedef mpl::bool_<boost::is_convertible<S, T>::value || gen_type::value> type;
#endif
};

template<typename From, typename To>
struct is_explicitly_convertible : public is_explicitly_convertible_imp<From, To>::type
{
};

}}} // namespaces

#endif