common_type_impl.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef BOOST_TYPE_TRAITS_DETAIL_COMMON_TYPE_IMPL_HPP_INCLUDED
  2. #define BOOST_TYPE_TRAITS_DETAIL_COMMON_TYPE_IMPL_HPP_INCLUDED
  3. //
  4. // Copyright 2015 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. //
  10. #include <boost/type_traits/detail/common_arithmetic_type.hpp>
  11. #include <boost/type_traits/detail/composite_pointer_type.hpp>
  12. #include <boost/type_traits/detail/composite_member_pointer_type.hpp>
  13. #include <boost/type_traits/type_identity.hpp>
  14. #include <boost/type_traits/is_class.hpp>
  15. #include <boost/type_traits/is_union.hpp>
  16. #include <boost/type_traits/is_convertible.hpp>
  17. #include <boost/type_traits/is_pointer.hpp>
  18. #include <boost/type_traits/is_member_pointer.hpp>
  19. #include <boost/type_traits/conditional.hpp>
  20. namespace boost
  21. {
  22. namespace type_traits_detail
  23. {
  24. // the arguments to common_type_impl have already been passed through decay<>
  25. template<class T, class U> struct common_type_impl;
  26. // same type
  27. template<class T> struct common_type_impl<T, T>
  28. {
  29. typedef T type;
  30. };
  31. // one of the operands is a class type, try conversions in both directions
  32. template<class T, class U> struct ct_class
  33. {
  34. BOOST_STATIC_CONSTANT( bool, ct = boost::is_class<T>::value || boost::is_union<T>::value );
  35. BOOST_STATIC_CONSTANT( bool, cu = boost::is_class<U>::value || boost::is_union<U>::value );
  36. BOOST_STATIC_CONSTANT( bool, value = ct || cu );
  37. };
  38. template<class T, class U> struct common_type_impl3;
  39. template<class T, class U> struct common_type_class: public boost::conditional<
  40. boost::is_convertible<T, U>::value && !boost::is_convertible<U, T>::value,
  41. boost::type_identity<U>,
  42. typename boost::conditional<
  43. boost::is_convertible<U, T>::value && !boost::is_convertible<T, U>::value,
  44. boost::type_identity<T>,
  45. common_type_impl3<T, U>
  46. >::type
  47. >::type
  48. {
  49. };
  50. template<class T, class U> struct common_type_impl: public boost::conditional<
  51. ct_class<T, U>::value,
  52. common_type_class<T, U>,
  53. common_type_impl3<T, U> >::type
  54. {
  55. };
  56. // pointers
  57. template<class T, class U> struct common_type_impl4;
  58. template<class T, class U> struct common_type_impl3: public boost::conditional<
  59. boost::is_pointer<T>::value || boost::is_pointer<U>::value,
  60. composite_pointer_type<T, U>,
  61. common_type_impl4<T, U> >::type
  62. {
  63. };
  64. // pointers to members
  65. template<class T, class U> struct common_type_impl5;
  66. template<class T, class U> struct common_type_impl4: public boost::conditional<
  67. boost::is_member_pointer<T>::value || boost::is_member_pointer<U>::value,
  68. composite_member_pointer_type<T, U>,
  69. common_type_impl5<T, U> >::type
  70. {
  71. };
  72. // arithmetic types (including class types w/ conversions to arithmetic and enums)
  73. template<class T, class U> struct common_type_impl5: public common_arithmetic_type<T, U>
  74. {
  75. };
  76. } // namespace type_traits_detail
  77. } // namespace boost
  78. #endif // #ifndef BOOST_TYPE_TRAITS_DETAIL_COMMON_TYPE_IMPL_HPP_INCLUDED