tuple.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 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...> )
  29. -> decltype( std::forward<F>(f)( std::get<J>(std::forward<Tp>(tp))... ) )
  30. {
  31. return std::forward<F>(f)( std::get<J>(std::forward<Tp>(tp))... );
  32. }
  33. } // namespace detail
  34. template<class F, class Tp,
  35. class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
  36. BOOST_MP11_CONSTEXPR auto tuple_apply( F && f, Tp && tp )
  37. -> decltype( detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() ) )
  38. {
  39. return detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() );
  40. }
  41. // construct_from_tuple
  42. namespace detail
  43. {
  44. 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...> )
  45. {
  46. return T( std::get<J>(std::forward<Tp>(tp))... );
  47. }
  48. } // namespace detail
  49. template<class T, class Tp,
  50. class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
  51. BOOST_MP11_CONSTEXPR T construct_from_tuple( Tp && tp )
  52. {
  53. return detail::construct_from_tuple_impl<T>( std::forward<Tp>(tp), Seq() );
  54. }
  55. // tuple_for_each
  56. namespace detail
  57. {
  58. 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 )
  59. {
  60. using A = int[sizeof...(J)];
  61. return (void)A{ ((void)f(std::get<J>(std::forward<Tp>(tp))), 0)... }, std::forward<F>(f);
  62. }
  63. template<class Tp, class F> BOOST_MP11_CONSTEXPR F tuple_for_each_impl( Tp && /*tp*/, integer_sequence<std::size_t>, F && f )
  64. {
  65. return std::forward<F>(f);
  66. }
  67. } // namespace detail
  68. template<class Tp, class F> BOOST_MP11_CONSTEXPR F tuple_for_each( Tp && tp, F && f )
  69. {
  70. using seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>;
  71. return detail::tuple_for_each_impl( std::forward<Tp>(tp), seq(), std::forward<F>(f) );
  72. }
  73. // tuple_transform
  74. namespace detail
  75. {
  76. // std::forward_as_tuple is not constexpr in C++11 or libstdc++ 5.x
  77. template<class... T> BOOST_MP11_CONSTEXPR auto tp_forward_r( T&&... t ) -> std::tuple<T&&...>
  78. {
  79. return std::tuple<T&&...>( std::forward<T>( t )... );
  80. }
  81. template<class... T> BOOST_MP11_CONSTEXPR auto tp_forward_v( T&&... t ) -> std::tuple<T...>
  82. {
  83. return std::tuple<T...>( std::forward<T>( t )... );
  84. }
  85. template<std::size_t J, class... Tp>
  86. BOOST_MP11_CONSTEXPR auto tp_extract( Tp&&... tp )
  87. -> decltype( tp_forward_r( std::get<J>( std::forward<Tp>( tp ) )... ) )
  88. {
  89. return tp_forward_r( std::get<J>( std::forward<Tp>( tp ) )... );
  90. }
  91. #if !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  92. template<class F, class... Tp, std::size_t... J>
  93. BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp&&... tp )
  94. -> decltype( tp_forward_v( tuple_apply( f, tp_extract<J>( std::forward<Tp>(tp)... ) )... ) )
  95. {
  96. return tp_forward_v( tuple_apply( f, tp_extract<J>( std::forward<Tp>(tp)... ) )... );
  97. }
  98. #else
  99. template<class F, class Tp1, std::size_t... J>
  100. BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1 )
  101. -> decltype( tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ) )... ) )
  102. {
  103. return tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ) )... );
  104. }
  105. template<class F, class Tp1, class Tp2, std::size_t... J>
  106. BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1, Tp2&& tp2 )
  107. -> decltype( tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ), std::get<J>( std::forward<Tp2>(tp2) ) )... ) )
  108. {
  109. return tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ), std::get<J>( std::forward<Tp2>(tp2) ) )... );
  110. }
  111. template<class F, class Tp1, class Tp2, class Tp3, std::size_t... J>
  112. BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1, Tp2&& tp2, Tp3&& tp3 )
  113. -> decltype( tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ), std::get<J>( std::forward<Tp2>(tp2) ), std::get<J>( std::forward<Tp3>(tp3) ) )... ) )
  114. {
  115. return tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ), std::get<J>( std::forward<Tp2>(tp2) ), std::get<J>( std::forward<Tp3>(tp3) ) )... );
  116. }
  117. #endif // !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  118. } // namespace detail
  119. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1910 )
  120. template<class F, class Tp1, class... Tp,
  121. class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp1>::type>::value>>
  122. BOOST_MP11_CONSTEXPR auto tuple_transform( F const& f, Tp1&& tp1, Tp&&... tp )
  123. -> decltype( detail::tuple_transform_impl( Seq(), f, std::forward<Tp1>(tp1), std::forward<Tp>(tp)... ) )
  124. {
  125. return detail::tuple_transform_impl( Seq(), f, std::forward<Tp1>(tp1), std::forward<Tp>(tp)... );
  126. }
  127. #else
  128. template<class F, class... Tp,
  129. class Z = mp_list<mp_size_t<std::tuple_size<typename std::remove_reference<Tp>::type>::value>...>,
  130. class E = mp_if<mp_apply<mp_same, Z>, mp_front<Z>>,
  131. class Seq = make_index_sequence<E::value>>
  132. BOOST_MP11_CONSTEXPR auto tuple_transform( F const& f, Tp&&... tp )
  133. -> decltype( detail::tuple_transform_impl( Seq(), f, std::forward<Tp>(tp)... ) )
  134. {
  135. return detail::tuple_transform_impl( Seq(), f, std::forward<Tp>(tp)... );
  136. }
  137. #endif // BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1910 )
  138. } // namespace mp11
  139. } // namespace boost
  140. #if BOOST_MP11_MSVC
  141. # pragma warning( pop )
  142. #endif
  143. #endif // #ifndef BOOST_TUPLE_HPP_INCLUDED