tuple.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #ifndef BOOST_MP11_TUPLE_HPP_INCLUDED
  2. #define BOOST_MP11_TUPLE_HPP_INCLUDED
  3. // Copyright 2015-2020 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <boost/mp11/integer_sequence.hpp>
  10. #include <boost/mp11/list.hpp>
  11. #include <boost/mp11/function.hpp>
  12. #include <boost/mp11/detail/config.hpp>
  13. #include <tuple>
  14. #include <utility>
  15. #include <type_traits>
  16. #include <cstddef>
  17. #if BOOST_MP11_MSVC
  18. # pragma warning( push )
  19. # pragma warning( disable: 4100 ) // unreferenced formal parameter 'tp'
  20. #endif
  21. namespace boost
  22. {
  23. namespace mp11
  24. {
  25. // tuple_apply
  26. namespace detail
  27. {
  28. using std::get;
  29. template<class F, class Tp, std::size_t... J> BOOST_MP11_CONSTEXPR auto tuple_apply_impl( F && f, Tp && tp, integer_sequence<std::size_t, J...> )
  30. -> decltype( std::forward<F>(f)( get<J>(std::forward<Tp>(tp))... ) )
  31. {
  32. return std::forward<F>(f)( get<J>(std::forward<Tp>(tp))... );
  33. }
  34. } // namespace detail
  35. template<class F, class Tp,
  36. class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
  37. BOOST_MP11_CONSTEXPR auto tuple_apply( F && f, Tp && tp )
  38. -> decltype( detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() ) )
  39. {
  40. return detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() );
  41. }
  42. // construct_from_tuple
  43. namespace detail
  44. {
  45. template<class T, class Tp, std::size_t... J> BOOST_MP11_CONSTEXPR T construct_from_tuple_impl( Tp && tp, integer_sequence<std::size_t, J...> )
  46. {
  47. return T( get<J>(std::forward<Tp>(tp))... );
  48. }
  49. } // namespace detail
  50. template<class T, class Tp,
  51. class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
  52. BOOST_MP11_CONSTEXPR T construct_from_tuple( Tp && tp )
  53. {
  54. return detail::construct_from_tuple_impl<T>( std::forward<Tp>(tp), Seq() );
  55. }
  56. // tuple_for_each
  57. namespace detail
  58. {
  59. template<class Tp, std::size_t... J, class F> BOOST_MP11_CONSTEXPR F tuple_for_each_impl( Tp && tp, integer_sequence<std::size_t, J...>, F && f )
  60. {
  61. using A = int[sizeof...(J)];
  62. return (void)A{ ((void)f(get<J>(std::forward<Tp>(tp))), 0)... }, std::forward<F>(f);
  63. }
  64. template<class Tp, class F> BOOST_MP11_CONSTEXPR F tuple_for_each_impl( Tp && /*tp*/, integer_sequence<std::size_t>, F && f )
  65. {
  66. return std::forward<F>(f);
  67. }
  68. } // namespace detail
  69. template<class Tp, class F> BOOST_MP11_CONSTEXPR F tuple_for_each( Tp && tp, F && f )
  70. {
  71. using seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>;
  72. return detail::tuple_for_each_impl( std::forward<Tp>(tp), seq(), std::forward<F>(f) );
  73. }
  74. // tuple_transform
  75. namespace detail
  76. {
  77. // std::forward_as_tuple is not constexpr in C++11 or libstdc++ 5.x
  78. template<class... T> BOOST_MP11_CONSTEXPR auto tp_forward_r( T&&... t ) -> std::tuple<T&&...>
  79. {
  80. return std::tuple<T&&...>( std::forward<T>( t )... );
  81. }
  82. template<class... T> BOOST_MP11_CONSTEXPR auto tp_forward_v( T&&... t ) -> std::tuple<T...>
  83. {
  84. return std::tuple<T...>( std::forward<T>( t )... );
  85. }
  86. template<std::size_t J, class... Tp>
  87. BOOST_MP11_CONSTEXPR auto tp_extract( Tp&&... tp )
  88. -> decltype( tp_forward_r( get<J>( std::forward<Tp>( tp ) )... ) )
  89. {
  90. return tp_forward_r( get<J>( std::forward<Tp>( tp ) )... );
  91. }
  92. #if !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  93. template<class F, class... Tp, std::size_t... J>
  94. BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp&&... tp )
  95. -> decltype( tp_forward_v( tuple_apply( f, tp_extract<J>( std::forward<Tp>(tp)... ) )... ) )
  96. {
  97. return tp_forward_v( tuple_apply( f, tp_extract<J>( std::forward<Tp>(tp)... ) )... );
  98. }
  99. #else
  100. template<class F, class Tp1, std::size_t... J>
  101. BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1 )
  102. -> decltype( tp_forward_v( f( get<J>( std::forward<Tp1>(tp1) ) )... ) )
  103. {
  104. return tp_forward_v( f( get<J>( std::forward<Tp1>(tp1) ) )... );
  105. }
  106. template<class F, class Tp1, class Tp2, std::size_t... J>
  107. BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1, Tp2&& tp2 )
  108. -> decltype( tp_forward_v( f( get<J>( std::forward<Tp1>(tp1) ), get<J>( std::forward<Tp2>(tp2) ) )... ) )
  109. {
  110. return tp_forward_v( f( get<J>( std::forward<Tp1>(tp1) ), get<J>( std::forward<Tp2>(tp2) ) )... );
  111. }
  112. template<class F, class Tp1, class Tp2, class Tp3, std::size_t... J>
  113. BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1, Tp2&& tp2, Tp3&& tp3 )
  114. -> decltype( tp_forward_v( f( get<J>( std::forward<Tp1>(tp1) ), get<J>( std::forward<Tp2>(tp2) ), get<J>( std::forward<Tp3>(tp3) ) )... ) )
  115. {
  116. return tp_forward_v( f( get<J>( std::forward<Tp1>(tp1) ), get<J>( std::forward<Tp2>(tp2) ), get<J>( std::forward<Tp3>(tp3) ) )... );
  117. }
  118. #endif // !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  119. } // namespace detail
  120. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1910 )
  121. template<class F, class Tp1, class... Tp,
  122. class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp1>::type>::value>>
  123. BOOST_MP11_CONSTEXPR auto tuple_transform( F const& f, Tp1&& tp1, Tp&&... tp )
  124. -> decltype( detail::tuple_transform_impl( Seq(), f, std::forward<Tp1>(tp1), std::forward<Tp>(tp)... ) )
  125. {
  126. return detail::tuple_transform_impl( Seq(), f, std::forward<Tp1>(tp1), std::forward<Tp>(tp)... );
  127. }
  128. #else
  129. template<class F, class... Tp,
  130. class Z = mp_list<mp_size_t<std::tuple_size<typename std::remove_reference<Tp>::type>::value>...>,
  131. class E = mp_if<mp_apply<mp_same, Z>, mp_front<Z>>,
  132. class Seq = make_index_sequence<E::value>>
  133. BOOST_MP11_CONSTEXPR auto tuple_transform( F const& f, Tp&&... tp )
  134. -> decltype( detail::tuple_transform_impl( Seq(), f, std::forward<Tp>(tp)... ) )
  135. {
  136. return detail::tuple_transform_impl( Seq(), f, std::forward<Tp>(tp)... );
  137. }
  138. #endif // BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1910 )
  139. } // namespace mp11
  140. } // namespace boost
  141. #if BOOST_MP11_MSVC
  142. # pragma warning( pop )
  143. #endif
  144. #endif // #ifndef BOOST_TUPLE_HPP_INCLUDED