1
0
forked from boostorg/mp11

Add tuple_apply

This commit is contained in:
Peter Dimov
2017-06-08 17:19:28 +03:00
parent 97720ed72d
commit 359ae25630
8 changed files with 323 additions and 1 deletions

View File

@@ -591,6 +591,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
</li>
<li><a href="#tuple">Tuple Operations, &lt;boost/mp11/tuple.hpp&gt;</a>
<ul class="sectlevel3">
<li><a href="#tuple_apply_f_tp">tuple_apply(f, tp)</a></li>
<li><a href="#tuple_for_each_tp_f">tuple_for_each(tp, f)</a></li>
</ul>
</li>
@@ -2809,6 +2810,18 @@ of <code>U&#8230;&#8203;</code> and the <code>mp_bind</code> expressions replace
<div class="sect2">
<h3 id="tuple">Tuple Operations, &lt;boost/mp11/tuple.hpp&gt;</h3>
<div class="sect3">
<h4 id="tuple_apply_f_tp">tuple_apply(f, tp)</h4>
<div class="literalblock">
<div class="content">
<pre>template&lt;class F, class Tp&gt; constexpr /*...*/ tuple_apply(F&amp;&amp; f, Tp&amp;&amp; tp);</pre>
</div>
</div>
<div class="paragraph">
<p><code>tuple_apply(f, tp)</code> returns <code>std::forward&lt;F&gt;(f)(std::get&lt;J&gt;(std::forward&lt;Tp&gt;(tp))&#8230;&#8203;)</code> for <code>J</code> in 0..<code>N-1</code>,
where <code>N</code> is <code>std::tuple_size&lt;typename std::remove_reference&lt;Tp&gt;::type&gt;::value</code>. Same as <code>std::apply</code> in C++17.</p>
</div>
</div>
<div class="sect3">
<h4 id="tuple_for_each_tp_f">tuple_for_each(tp, f)</h4>
<div class="literalblock">
<div class="content">

View File

@@ -9,8 +9,17 @@ http://www.boost.org/LICENSE_1_0.txt
[#tuple]
# Tuple Operations, <boost/mp11/tuple.hpp>
:toc:
:toc-title:
:idprefix:
## tuple_apply(f, tp)
template<class F, class Tp> constexpr /*...*/ tuple_apply(F&& f, Tp&& tp);
`tuple_apply(f, tp)` returns `std::forward<F>(f)(std::get<J>(std::forward<Tp>(tp))...)` for `J` in 0..`N-1`,
where `N` is `std::tuple_size<typename std::remove_reference<Tp>::type>::value`. Same as `std::apply` in C++17.
## tuple_for_each(tp, f)
template<class Tp, class F> constexpr F tuple_for_each(Tp&& tp, F&& f);

View File

@@ -21,6 +21,26 @@ namespace boost
namespace mp11
{
// tuple_apply
namespace detail
{
template<class F, class Tp, std::size_t... J> BOOST_CONSTEXPR auto tuple_apply_impl( F && f, Tp && tp, integer_sequence<std::size_t, J...> )
-> decltype( std::forward<F>(f)( std::get<J>(std::forward<Tp>(tp))... ) )
{
return std::forward<F>(f)( std::get<J>(std::forward<Tp>(tp))... );
}
} // namespace detail
template<class F, class Tp,
class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
BOOST_CONSTEXPR auto tuple_apply( F && f, Tp && tp )
-> decltype( detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() ) )
{
return detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() );
}
// tuple_for_each
namespace detail
{

View File

@@ -2675,6 +2675,26 @@ namespace boost
namespace mp11
{
// tuple_apply
namespace detail
{
template<class F, class Tp, std::size_t... J> BOOST_CONSTEXPR auto tuple_apply_impl( F && f, Tp && tp, integer_sequence<std::size_t, J...> )
-> decltype( std::forward<F>(f)( std::get<J>(std::forward<Tp>(tp))... ) )
{
return std::forward<F>(f)( std::get<J>(std::forward<Tp>(tp))... );
}
} // namespace detail
template<class F, class Tp,
class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
BOOST_CONSTEXPR auto tuple_apply( F && f, Tp && tp )
-> decltype( detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() ) )
{
return detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() );
}
// tuple_for_each
namespace detail
{

View File

@@ -93,9 +93,11 @@ run mp_invoke_sf.cpp : : : $(REQ) ;
# integer_sequence
run integer_sequence.cpp : : : $(REQ) ;
# tuple_for_each
# tuple
run tuple_for_each.cpp : : : $(REQ) ;
run tuple_for_each_cx.cpp : : : $(REQ) ;
run tuple_apply.cpp : : : $(REQ) ;
run tuple_apply_cx.cpp : : : $(REQ) ;
# set
run mp_set_contains.cpp : : : $(REQ) ;

132
test/tuple_apply.cpp Normal file
View File

@@ -0,0 +1,132 @@
// Copyright 2015, 2017 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/mp11/tuple.hpp>
#include <boost/core/lightweight_test.hpp>
#include <tuple>
#include <memory>
#include <utility>
#include <array>
int main()
{
using boost::mp11::tuple_apply;
{
std::tuple<int, short, char> tp{ 1, 2, 3 };
{
int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
BOOST_TEST_EQ( s, 123 );
}
{
int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
BOOST_TEST_EQ( s, 123 );
}
}
{
std::tuple<int, short, char> const tp{ 1, 2, 3 };
{
int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
BOOST_TEST_EQ( s, 123 );
}
{
int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
BOOST_TEST_EQ( s, 123 );
}
}
#if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 8
#else
{
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 = tuple_apply( [&]( std::unique_ptr<int> px, std::unique_ptr<int> py, std::unique_ptr<int> pz ){ return 100 * *px + 10 * *py + *pz; }, std::move(tp) );
BOOST_TEST_EQ( s, 123 );
}
#endif
{
std::pair<int, short> tp{ 1, 2 };
{
int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, tp );
BOOST_TEST_EQ( s, 12 );
}
{
int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, std::move(tp) );
BOOST_TEST_EQ( s, 12 );
}
}
{
std::pair<int, short> const tp{ 1, 2 };
{
int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, tp );
BOOST_TEST_EQ( s, 12 );
}
{
int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, std::move(tp) );
BOOST_TEST_EQ( s, 12 );
}
}
{
std::array<int, 3> tp{{ 1, 2, 3 }};
{
int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
BOOST_TEST_EQ( s, 123 );
}
{
int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
BOOST_TEST_EQ( s, 123 );
}
}
{
std::array<int, 3> const tp{{ 1, 2, 3 }};
{
int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
BOOST_TEST_EQ( s, 123 );
}
{
int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
BOOST_TEST_EQ( s, 123 );
}
}
{
std::tuple<> tp;
BOOST_TEST_EQ( tuple_apply( []{ return 11; }, tp ), 11 );
BOOST_TEST_EQ( tuple_apply( []{ return 12; }, std::move( tp ) ), 12 );
}
{
std::array<int, 0> tp;
BOOST_TEST_EQ( tuple_apply( []{ return 11; }, tp ), 11 );
BOOST_TEST_EQ( tuple_apply( []{ return 12; }, std::move( tp ) ), 12 );
}
return boost::report_errors();
}

66
test/tuple_apply_cx.cpp Normal file
View File

@@ -0,0 +1,66 @@
// 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/mp11/tuple.hpp>
#include <boost/config.hpp>
// Technically std::tuple isn't constexpr enabled in C++11, but it works with libstdc++
#if defined( BOOST_NO_CXX11_CONSTEXPR ) || ( defined( _LIBCPP_VERSION ) && __cplusplus < 201400L )
int main() {}
#else
#include <tuple>
#include <array>
#include <utility>
constexpr int f( int x, int y, int z )
{
return x * 100 + y * 10 + z;
}
constexpr int g( int x, int y )
{
return x * 10 + y;
}
constexpr int h()
{
return 11;
}
int main()
{
{
constexpr std::tuple<int, short, char> tp{ 1, 2, 3 };
constexpr auto r = boost::mp11::tuple_apply( f, tp );
static_assert( r == 123, "r == 123" );
}
{
constexpr std::pair<short, char> tp{ 1, 2 };
constexpr auto r = boost::mp11::tuple_apply( g, tp );
static_assert( r == 12, "r == 12" );
}
{
constexpr std::array<short, 3> tp{{ 1, 2, 3 }};
constexpr auto r = boost::mp11::tuple_apply( f, tp );
static_assert( r == 123, "r == 123" );
}
{
constexpr std::tuple<> tp;
constexpr auto r = boost::mp11::tuple_apply( h, tp );
static_assert( r == 11, "r == 11" );
}
}
#endif

View File

@@ -38,6 +38,26 @@ int main()
}
}
{
std::tuple<int, short, char> const tp{ 1, 2, 3 };
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 123 );
}
{
int s = 0;
tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 123 );
}
}
#if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 8
#else
@@ -73,6 +93,26 @@ int main()
}
}
{
std::pair<int, short> const tp{ 1, 2 };
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 12 );
}
{
int s = 0;
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 }};
@@ -93,6 +133,26 @@ int main()
}
}
{
std::array<int, 3> const tp{{ 1, 2, 3 }};
{
int s = 0;
tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 123 );
}
{
int s = 0;
tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
BOOST_TEST_EQ( s, 123 );
}
}
{
std::tuple<> tp;