forked from boostorg/mp11
Add tuple_for_each.
This commit is contained in:
33
include/boost/tuple_for_each.hpp
Normal file
33
include/boost/tuple_for_each.hpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef BOOST_TUPLE_FOR_EACH_HPP_INCLUDED
|
||||||
|
#define BOOST_TUPLE_FOR_EACH_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <boost/integer_sequence.hpp>
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#include <tuple>
|
||||||
|
#include <utility>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace boost
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
template<class Tp, std::size_t... J, class F> BOOST_CONSTEXPR F tuple_for_each_impl( Tp && tp, boost::integer_sequence<std::size_t, J...>, F && f )
|
||||||
|
{
|
||||||
|
using A = int[sizeof...(J)];
|
||||||
|
return (void)A{ (f(std::get<J>(std::forward<Tp>(tp))), 0)... }, std::forward<F>(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template<class Tp, class F> BOOST_CONSTEXPR F tuple_for_each( Tp && tp, F && f )
|
||||||
|
{
|
||||||
|
using seq = boost::make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>;
|
||||||
|
return detail::tuple_for_each_impl( std::forward<Tp>(tp), seq(), std::forward<F>(f) );
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
|
#endif // #ifndef BOOST_TUPLE_FOR_EACH_HPP_INCLUDED
|
@@ -52,3 +52,7 @@ run mp_eval_if.cpp : : : $(REQ) ;
|
|||||||
|
|
||||||
# integer_sequence
|
# integer_sequence
|
||||||
run integer_sequence.cpp : : : $(REQ) ;
|
run integer_sequence.cpp : : : $(REQ) ;
|
||||||
|
|
||||||
|
# tuple_for_each
|
||||||
|
run tuple_for_each.cpp : : : $(REQ) ;
|
||||||
|
run tuple_for_each_cx.cpp : : : $(REQ) ;
|
||||||
|
90
test/tuple_for_each.cpp
Normal file
90
test/tuple_for_each.cpp
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
|
||||||
|
// Copyright 2015 Peter Dimov.
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
//
|
||||||
|
// See accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
|
|
||||||
|
#include <boost/tuple_for_each.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <tuple>
|
||||||
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::tuple<int, short, char> tp{ 1, 2, 3 };
|
||||||
|
|
||||||
|
{
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
boost::tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( s, 123 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
boost::tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( s, 123 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::tuple<std::unique_ptr<int>, std::unique_ptr<int>, std::unique_ptr<int>> tp{ std::unique_ptr<int>(new int(1)), std::unique_ptr<int>(new int(2)), std::unique_ptr<int>(new int(3)) };
|
||||||
|
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
boost::tuple_for_each( std::move(tp), [&]( std::unique_ptr<int> p ){ s = s * 10 + *p; } );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( s, 123 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::pair<int, short> tp{ 1, 2 };
|
||||||
|
|
||||||
|
{
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
boost::tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( s, 12 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
boost::tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( s, 12 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<int, 3> tp{{ 1, 2, 3 }};
|
||||||
|
|
||||||
|
{
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
boost::tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( s, 123 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
boost::tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( s, 123 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
37
test/tuple_for_each_cx.cpp
Normal file
37
test/tuple_for_each_cx.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
// Copyright 2015 Peter Dimov.
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
//
|
||||||
|
// See accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
|
#include <boost/tuple_for_each.hpp>
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
|
||||||
|
#if defined( BOOST_NO_CXX11_CONSTEXPR )
|
||||||
|
|
||||||
|
int main() {}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
struct assert_is_integral
|
||||||
|
{
|
||||||
|
template<class T> constexpr bool operator()( T ) const
|
||||||
|
{
|
||||||
|
static_assert( std::is_integral<T>::value, "T must be an integral type" );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
constexpr std::tuple<int, short, char> tp{ 1, 2, 3 };
|
||||||
|
constexpr auto r = boost::tuple_for_each( tp, assert_is_integral() );
|
||||||
|
(void)r;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user