tuple_for_each.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef BOOST_BIND_DETAIL_TUPLE_FOR_EACH_HPP_INCLUDED
  2. #define BOOST_BIND_DETAIL_TUPLE_FOR_EACH_HPP_INCLUDED
  3. // Copyright 2015-2020, 2024 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/bind/detail/integer_sequence.hpp>
  10. #include <boost/config.hpp>
  11. #include <utility>
  12. #include <type_traits>
  13. #include <cstddef>
  14. #if defined(BOOST_MSVC)
  15. # pragma warning( push )
  16. # pragma warning( disable: 4100 ) // unreferenced formal parameter 'tp'
  17. #endif
  18. namespace boost
  19. {
  20. namespace _bi
  21. {
  22. // tuple_for_each( f, tp )
  23. template<class F, class Tp, std::size_t... J> F tuple_for_each_impl( F&& f, Tp&& tp, integer_sequence<std::size_t, J...> )
  24. {
  25. using A = int[ 1 + sizeof...(J) ];
  26. using std::get;
  27. return (void)A{ 0, ((void)f(get<J>(std::forward<Tp>(tp))), 0)... }, std::forward<F>(f);
  28. }
  29. template<class F, class Tp> F tuple_for_each( F&& f, Tp&& tp )
  30. {
  31. using seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>;
  32. return _bi::tuple_for_each_impl( std::forward<F>(f), std::forward<Tp>(tp), seq() );
  33. }
  34. // tuple_for_each( f, tp1, tp2 )
  35. template<class F, class Tp1, class Tp2, std::size_t... J> F tuple_for_each_impl( F&& f, Tp1&& tp1, Tp2&& tp2, integer_sequence<std::size_t, J...> )
  36. {
  37. using A = int[ 1 + sizeof...(J) ];
  38. using std::get;
  39. return (void)A{ 0, ((void)f( get<J>(std::forward<Tp1>(tp1)), get<J>(std::forward<Tp2>(tp2)) ), 0)... }, std::forward<F>(f);
  40. }
  41. template<class F, class Tp1, class Tp2> F tuple_for_each( F&& f, Tp1&& tp1, Tp2&& tp2 )
  42. {
  43. using seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp1>::type>::value>;
  44. return _bi::tuple_for_each_impl( std::forward<F>(f), std::forward<Tp1>(tp1), std::forward<Tp2>(tp2), seq() );
  45. }
  46. } // namespace _bi
  47. } // namespace boost
  48. #if defined(BOOST_MSVC)
  49. # pragma warning( pop )
  50. #endif
  51. #endif // #ifndef BOOST_BIND_DETAIL_TUPLE_FOR_EACH_HPP_INCLUDED