123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /*=============================================================================
- Copyright (c) 2017 Paul Fultz II
- tuple_for_each.cpp
- 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/hof/unpack.hpp>
- #include <boost/hof/proj.hpp>
- #include <boost/hof/function.hpp>
- #include <boost/hof/reveal.hpp>
- #include "test.hpp"
- struct tuple_for_each_f
- {
- template<class Sequence, class F>
- constexpr auto operator()(Sequence&& s, F && f) const BOOST_HOF_RETURNS
- (
- boost::hof::unpack(boost::hof::proj(boost::hof::forward<F>(f)))(boost::hof::forward<Sequence>(s)), boost::hof::forward<F>(f)
- );
- };
- BOOST_HOF_STATIC_FUNCTION(tuple_for_each) = tuple_for_each_f{};
- BOOST_HOF_TEST_CASE()
- {
- std::tuple<int, short, char> tp{ 1, 2, 3 };
- {
- int s = 0;
- tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- {
- int s = 0;
- tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- }
- BOOST_HOF_TEST_CASE()
- {
- std::tuple<int, short, char> const tp{ 1, 2, 3 };
- {
- int s = 0;
- tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- {
- int s = 0;
- tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- }
- // #if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 8
- // #else
- BOOST_HOF_TEST_CASE()
- {
- 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;
- tuple_for_each( std::move(tp), [&]( std::unique_ptr<int> p ){ s = s * 10 + *p; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- BOOST_HOF_TEST_CASE()
- {
- auto tp = boost::hof::pack(1, 2, 3);
- {
- int s = 0;
- tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- {
- int s = 0;
- tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- }
- BOOST_HOF_TEST_CASE()
- {
- const auto tp = boost::hof::pack(1, 2, 3);
- {
- int s = 0;
- tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- {
- int s = 0;
- tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- }
- // #endif
- BOOST_HOF_TEST_CASE()
- {
- std::pair<int, short> tp{ 1, 2 };
- {
- int s = 0;
- tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 12 );
- }
- {
- int s = 0;
- tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 12 );
- }
- }
- BOOST_HOF_TEST_CASE()
- {
- std::pair<int, short> const tp{ 1, 2 };
- {
- int s = 0;
- tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 12 );
- }
- {
- int s = 0;
- tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 12 );
- }
- }
- BOOST_HOF_TEST_CASE()
- {
- std::array<int, 3> tp{{ 1, 2, 3 }};
- {
- int s = 0;
- tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- {
- int s = 0;
- tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- }
- BOOST_HOF_TEST_CASE()
- {
- std::array<int, 3> const tp{{ 1, 2, 3 }};
- {
- int s = 0;
- tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- {
- int s = 0;
- tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
- BOOST_HOF_TEST_CHECK( s == 123 );
- }
- }
- BOOST_HOF_TEST_CASE()
- {
- std::tuple<> tp;
- BOOST_HOF_TEST_CHECK( tuple_for_each( tp, 11 ) == 11 );
- BOOST_HOF_TEST_CHECK( tuple_for_each( std::move( tp ), 12 ) == 12 );
- }
- BOOST_HOF_TEST_CASE()
- {
- BOOST_HOF_TEST_CHECK( tuple_for_each( boost::hof::pack(), 11 ) == 11 );
- BOOST_HOF_STATIC_TEST_CHECK( tuple_for_each( boost::hof::pack(), 11 ) == 11 );
- }
- BOOST_HOF_TEST_CASE()
- {
- std::array<int, 0> tp;
- BOOST_HOF_TEST_CHECK( tuple_for_each( tp, 11 ) == 11 );
- BOOST_HOF_TEST_CHECK( tuple_for_each( std::move( tp ), 12 ) == 12 );
- }
- struct assert_is_integral
- {
- template<class T> constexpr bool operator()( T ) const
- {
- BOOST_HOF_STATIC_TEST_CHECK( std::is_integral<T>::value );
- return true;
- }
- };
- BOOST_HOF_TEST_CASE()
- {
- #if !BOOST_HOF_HAS_CONSTEXPR_TUPLE
- auto r = tuple_for_each( std::tuple<int, short, char>{1, 2, 3}, assert_is_integral() );
- #else
- constexpr auto r = tuple_for_each( std::tuple<int, short, char>{1, 2, 3}, assert_is_integral() );
- #endif
- (void)r;
- }
- BOOST_HOF_TEST_CASE()
- {
- #if !BOOST_HOF_HAS_CONSTEXPR_TUPLE
- auto r = tuple_for_each( boost::hof::pack(1, 2, 3), assert_is_integral() );
- #else
- constexpr auto r = tuple_for_each( boost::hof::pack(1, 2, 3), assert_is_integral() );
- #endif
- (void)r;
- }
|