forked from boostorg/mp11
Add tuple_for_each.
This commit is contained in:
@@ -52,3 +52,7 @@ run mp_eval_if.cpp : : : $(REQ) ;
|
||||
|
||||
# integer_sequence
|
||||
run integer_sequence.cpp : : : $(REQ) ;
|
||||
|
||||
# tuple_for_each
|
||||
run tuple_for_each.cpp : : : $(REQ) ;
|
||||
run tuple_for_each_cx.cpp : : : $(REQ) ;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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