1
0
forked from boostorg/mp11

Add tuple_for_each.

This commit is contained in:
Peter Dimov
2015-06-24 14:57:32 +03:00
parent 4a33810f9b
commit 05ab3e7a42
4 changed files with 164 additions and 0 deletions

90
test/tuple_for_each.cpp Normal file
View 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();
}