common_type.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef BOOST_TYPE_TRAITS_COMMON_TYPE_HPP_INCLUDED
  2. #define BOOST_TYPE_TRAITS_COMMON_TYPE_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/config.hpp>
  11. #include <boost/type_traits/decay.hpp>
  12. #include <boost/type_traits/declval.hpp>
  13. #include <boost/detail/workaround.hpp>
  14. #include <boost/type_traits/is_complete.hpp>
  15. #include <boost/type_traits/is_void.hpp>
  16. #include <boost/type_traits/is_array.hpp>
  17. #include <boost/static_assert.hpp>
  18. #if defined(BOOST_NO_CXX11_DECLTYPE)
  19. #include <boost/type_traits/detail/common_type_impl.hpp>
  20. #endif
  21. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  22. #include <boost/type_traits/detail/mp_defer.hpp>
  23. #endif
  24. namespace boost
  25. {
  26. // variadic common_type
  27. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  28. template<class... T> struct common_type
  29. {
  30. };
  31. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  32. template<class... T> using common_type_t = typename common_type<T...>::type;
  33. namespace type_traits_detail
  34. {
  35. template<class T1, class T2, class... T> using common_type_fold = common_type_t<common_type_t<T1, T2>, T...>;
  36. } // namespace type_traits_detail
  37. template<class T1, class T2, class... T>
  38. struct common_type<T1, T2, T...>: type_traits_detail::mp_defer<type_traits_detail::common_type_fold, T1, T2, T...>
  39. {
  40. };
  41. #else
  42. template<class T1, class T2, class... T>
  43. struct common_type<T1, T2, T...>: common_type<typename common_type<T1, T2>::type, T...>
  44. {
  45. };
  46. #endif // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  47. #else
  48. template<
  49. class T1 = void, class T2 = void, class T3 = void,
  50. class T4 = void, class T5 = void, class T6 = void,
  51. class T7 = void, class T8 = void, class T9 = void
  52. >
  53. struct common_type: common_type<typename common_type<T1, T2>::type, T3, T4, T5, T6, T7, T8, T9>
  54. {
  55. };
  56. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  57. // one argument
  58. template<class T> struct common_type<T>: boost::decay<T>
  59. {
  60. BOOST_STATIC_ASSERT_MSG(::boost::is_complete<T>::value || ::boost::is_void<T>::value || ::boost::is_array<T>::value, "Arguments to common_type must both be complete types");
  61. };
  62. // two arguments
  63. namespace type_traits_detail
  64. {
  65. // binary common_type
  66. #if !defined(BOOST_NO_CXX11_DECLTYPE)
  67. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  68. #if !defined(BOOST_MSVC) || BOOST_MSVC > 1800
  69. // internal compiler error on msvc-12.0
  70. template<class T1, class T2> using builtin_common_type = typename boost::decay<decltype( boost::declval<bool>()? boost::declval<T1>(): boost::declval<T2>() )>::type;
  71. template<class T1, class T2> struct common_type_impl: mp_defer<builtin_common_type, T1, T2>
  72. {
  73. };
  74. #else
  75. template<class T1, class T2> using builtin_common_type = decltype( boost::declval<bool>()? boost::declval<T1>(): boost::declval<T2>() );
  76. template<class T1, class T2> struct common_type_impl_2: mp_defer<builtin_common_type, T1, T2>
  77. {
  78. };
  79. template<class T1, class T2> using decay_common_type = typename boost::decay<typename common_type_impl_2<T1, T2>::type>::type;
  80. template<class T1, class T2> struct common_type_impl: mp_defer<decay_common_type, T1, T2>
  81. {
  82. };
  83. #endif // !defined(BOOST_MSVC) || BOOST_MSVC > 1800
  84. #else
  85. template<class T1, class T2> struct common_type_impl: boost::decay<decltype( boost::declval<bool>()? boost::declval<T1>(): boost::declval<T2>() )>
  86. {
  87. };
  88. #endif // #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  89. #endif // #if !defined(BOOST_NO_CXX11_DECLTYPE)
  90. // decay helper
  91. template<class T1, class T2, class T1d = typename boost::decay<T1>::type, class T2d = typename boost::decay<T2>::type> struct common_type_decay_helper: boost::common_type<T1d, T2d>
  92. {
  93. };
  94. template<class T1, class T2> struct common_type_decay_helper<T1, T2, T1, T2>: common_type_impl<T1, T2>
  95. {
  96. };
  97. } // type_traits_detail
  98. template<class T1, class T2> struct common_type<T1, T2>: type_traits_detail::common_type_decay_helper<T1, T2>
  99. {
  100. BOOST_STATIC_ASSERT_MSG(::boost::is_complete<T1>::value || ::boost::is_void<T1>::value || ::boost::is_array<T1>::value, "Arguments to common_type must both be complete types");
  101. BOOST_STATIC_ASSERT_MSG(::boost::is_complete<T2>::value || ::boost::is_void<T2>::value || ::boost::is_array<T2>::value, "Arguments to common_type must both be complete types");
  102. };
  103. } // namespace boost
  104. #endif // #ifndef BOOST_TYPE_TRAITS_COMMON_TYPE_HPP_INCLUDED