1
0
forked from boostorg/mp11

Merge branch 'develop'

This commit is contained in:
Peter Dimov
2017-06-08 20:49:51 +03:00
16 changed files with 779 additions and 87 deletions

View File

@@ -46,7 +46,7 @@ Tested on [Travis](https://travis-ci.org/pdimov/mp11/) and [Appveyor](https://ci
* [Integer Sequences](doc/mp11/integer_sequence.adoc)
* [A "for each" algorithm for tuple-like types](doc/mp11/tuple_for_each.adoc)
* [Tuple Operations](doc/mp11/tuple.adoc)
## License

View File

@@ -589,8 +589,10 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
<li><a href="#index_sequence_for_t">index_sequence_for&lt;T&#8230;&#8203;&gt;</a></li>
</ul>
</li>
<li><a href="#tuple_for_each">A "for each" algorithm for tuple-like types, &lt;boost/mp11/tuple_for_each.hpp&gt;</a>
<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="#construct_from_tuple_t_tp">construct_from_tuple&lt;T&gt;(tp)</a></li>
<li><a href="#tuple_for_each_tp_f">tuple_for_each(tp, f)</a></li>
</ul>
</li>
@@ -2807,7 +2809,32 @@ of <code>U&#8230;&#8203;</code> and the <code>mp_bind</code> expressions replace
</div>
</div>
<div class="sect2">
<h3 id="tuple_for_each">A "for each" algorithm for tuple-like types, &lt;boost/mp11/tuple_for_each.hpp&gt;</h3>
<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="construct_from_tuple_t_tp">construct_from_tuple&lt;T&gt;(tp)</h4>
<div class="literalblock">
<div class="content">
<pre>template&lt;class T, class Tp&gt; T construct_from_tuple(Tp&amp;&amp; tp);</pre>
</div>
</div>
<div class="paragraph">
<p><code>construct_from_tuple&lt;T&gt;(tp)</code> returns <code>T(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::make_from_tuple</code> in C++17.
The name of the function doesn&#8217;t match the C++17 one to avoid ambiguities when both are visible or in unqualified calls.</p>
</div>
</div>
<div class="sect3">
<h4 id="tuple_for_each_tp_f">tuple_for_each(tp, f)</h4>
<div class="literalblock">
@@ -2847,7 +2874,7 @@ expression <code>f(std::get&lt;J&gt;(std::forward&lt;Tp&gt;(tp)))</code> for <co
</div>
<div id="footer">
<div id="footer-text">
Last updated 2017-06-07 00:11:03 GTBDT
Last updated 2017-06-08 04:54:36 GTBDT
</div>
</div>
</body>

View File

@@ -35,6 +35,6 @@ include::bind.adoc[]
include::integer_sequence.adoc[]
include::tuple_for_each.adoc[]
include::tuple.adoc[]
:leveloffset: -1

38
doc/mp11/tuple.adoc Normal file
View File

@@ -0,0 +1,38 @@
////
Copyright 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
////
[#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.
## construct_from_tuple<T>(tp)
template<class T, class Tp> T construct_from_tuple(Tp&& tp);
`construct_from_tuple<T>(tp)` returns `T(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::make_from_tuple` in {cpp}17.
The name of the function doesn't match the {cpp}17 one to avoid ambiguities when both are visible or in unqualified calls.
## tuple_for_each(tp, f)
template<class Tp, class F> constexpr F tuple_for_each(Tp&& tp, F&& f);
`tuple_for_each(tp, f)` applies the function object `f` to each element of `tp` by evaluating the
expression `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`.
Returns `std::forward<F>(f)`.

View File

@@ -1,21 +0,0 @@
////
Copyright 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
////
[#tuple_for_each]
# A "for each" algorithm for tuple-like types, <boost/mp11/tuple_for_each.hpp>
:idprefix:
## tuple_for_each(tp, f)
template<class Tp, class F> constexpr F tuple_for_each(Tp&& tp, F&& f);
`tuple_for_each(tp, f)` applies the function object `f` to each element of `tp` by evaluating the
expression `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`.
Returns `std::forward<F>(f)`.

View File

@@ -17,6 +17,6 @@
#include <boost/mp11/set.hpp>
#include <boost/mp11/bind.hpp>
#include <boost/mp11/integer_sequence.hpp>
#include <boost/mp11/tuple_for_each.hpp>
#include <boost/mp11/tuple.hpp>
#endif // #ifndef BOOST_MP11_HPP_INCLUDED

View File

@@ -0,0 +1,92 @@
#ifndef BOOST_MP11_TUPLE_HPP_INCLUDED
#define BOOST_MP11_TUPLE_HPP_INCLUDED
// 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/integer_sequence.hpp>
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <tuple>
#include <utility>
#include <type_traits>
#include <cstddef>
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() );
}
// construct_from_tuple
namespace detail
{
template<class T, class Tp, std::size_t... J> BOOST_CONSTEXPR T construct_from_tuple_impl( Tp && tp, integer_sequence<std::size_t, J...> )
{
return T( std::get<J>(std::forward<Tp>(tp))... );
}
} // namespace detail
template<class T, class Tp,
class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
BOOST_CONSTEXPR T construct_from_tuple( Tp && tp )
{
return detail::construct_from_tuple_impl<T>( std::forward<Tp>(tp), Seq() );
}
// tuple_for_each
namespace detail
{
template<class Tp, std::size_t... J, class F> BOOST_CONSTEXPR F tuple_for_each_impl( Tp && tp, integer_sequence<std::size_t, J...>, F && f )
{
using A = int[sizeof...(J)];
return (void)A{ ((void)f(std::get<J>(std::forward<Tp>(tp))), 0)... }, std::forward<F>(f);
}
#if BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
template<class Tp, class F> BOOST_CONSTEXPR F tuple_for_each_impl( Tp && tp, integer_sequence<std::size_t>, F && f )
{
return std::forward<F>(f);
}
#endif
} // namespace detail
template<class Tp, class F> BOOST_CONSTEXPR F tuple_for_each( Tp && tp, F && f )
{
using seq = 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 mp11
} // namespace boost
#endif // #ifndef BOOST_TUPLE_HPP_INCLUDED

View File

@@ -1,53 +0,0 @@
#ifndef BOOST_MP11_TUPLE_FOR_EACH_HPP_INCLUDED
#define BOOST_MP11_TUPLE_FOR_EACH_HPP_INCLUDED
// 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/integer_sequence.hpp>
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <tuple>
#include <utility>
#include <type_traits>
#include <cstddef>
namespace boost
{
namespace mp11
{
namespace detail
{
template<class Tp, std::size_t... J, class F> BOOST_CONSTEXPR F tuple_for_each_impl( Tp && tp, integer_sequence<std::size_t, J...>, F && f )
{
using A = int[sizeof...(J)];
return (void)A{ ((void)f(std::get<J>(std::forward<Tp>(tp))), 0)... }, std::forward<F>(f);
}
#if BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
template<class Tp, class F> BOOST_CONSTEXPR F tuple_for_each_impl( Tp && tp, integer_sequence<std::size_t>, F && f )
{
return std::forward<F>(f);
}
#endif
} // namespace detail
template<class Tp, class F> BOOST_CONSTEXPR F tuple_for_each( Tp && tp, F && f )
{
using seq = 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 mp11
} // namespace boost
#endif // #ifndef BOOST_TUPLE_FOR_EACH_HPP_INCLUDED

View File

@@ -2653,8 +2653,8 @@ template<class Q, class... T> using mp_bind_back_q = mp_bind_back<Q::template fn
} // namespace boost
#endif // #ifndef BOOST_MP11_BIND_HPP_INCLUDED
#ifndef BOOST_MP11_TUPLE_FOR_EACH_HPP_INCLUDED
#define BOOST_MP11_TUPLE_FOR_EACH_HPP_INCLUDED
#ifndef BOOST_MP11_TUPLE_HPP_INCLUDED
#define BOOST_MP11_TUPLE_HPP_INCLUDED
// Copyright 2015, 2017 Peter Dimov.
//
@@ -2675,6 +2675,45 @@ 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() );
}
// construct_from_tuple
namespace detail
{
template<class T, class Tp, std::size_t... J> BOOST_CONSTEXPR T construct_from_tuple_impl( Tp && tp, integer_sequence<std::size_t, J...> )
{
return T( std::get<J>(std::forward<Tp>(tp))... );
}
} // namespace detail
template<class T, class Tp,
class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
BOOST_CONSTEXPR T construct_from_tuple( Tp && tp )
{
return detail::construct_from_tuple_impl<T>( std::forward<Tp>(tp), Seq() );
}
// tuple_for_each
namespace detail
{
@@ -2704,6 +2743,6 @@ template<class Tp, class F> BOOST_CONSTEXPR F tuple_for_each( Tp && tp, F && f )
} // namespace mp11
} // namespace boost
#endif // #ifndef BOOST_TUPLE_FOR_EACH_HPP_INCLUDED
#endif // #ifndef BOOST_TUPLE_HPP_INCLUDED
#endif // #ifndef BOOST_MP11_HPP_INCLUDED

View File

@@ -93,9 +93,13 @@ 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) ;
compile tuple_for_each_cx.cpp : : : $(REQ) ;
run tuple_apply.cpp : : : $(REQ) ;
compile tuple_apply_cx.cpp : : : $(REQ) ;
run construct_from_tuple.cpp : : : $(REQ) ;
compile construct_from_tuple_cx.cpp : : : $(REQ) ;
# set
run mp_set_contains.cpp : : : $(REQ) ;

View File

@@ -0,0 +1,219 @@
// 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 <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <tuple>
#include <memory>
#include <utility>
#include <array>
struct T1
{
int x, y, z;
T1( int x = 0, int y = 0, int z = 0 ): x(x), y(y), z(z) {}
};
struct T2
{
std::unique_ptr<int> x, y, z;
T2( std::unique_ptr<int> x, std::unique_ptr<int> y, std::unique_ptr<int> z ): x(std::move(x)), y(std::move(y)), z(std::move(z)) {}
#if BOOST_WORKAROUND( BOOST_MSVC, <= 1800 )
T2( T2&& r ): x( std::move(r.x) ), y( std::move(r.y) ), z( std::move(r.z) ) {}
#endif
};
int main()
{
using boost::mp11::construct_from_tuple;
{
std::tuple<int, short, char> tp{ 1, 2, 3 };
{
T1 t1 = construct_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
{
T1 t1 = construct_from_tuple<T1>( std::move(tp) );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
}
{
std::tuple<int, short, char> const tp{ 1, 2, 3 };
{
T1 t1 = construct_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
{
T1 t1 = construct_from_tuple<T1>( std::move(tp) );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
}
#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)) };
T2 t2 = construct_from_tuple<T2>( std::move(tp) );
BOOST_TEST_EQ( *t2.x, 1 );
BOOST_TEST_EQ( *t2.y, 2 );
BOOST_TEST_EQ( *t2.z, 3 );
}
#endif
{
std::pair<int, short> tp{ 1, 2 };
{
T1 t1 = construct_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 0 );
}
{
T1 t1 = construct_from_tuple<T1>( std::move(tp) );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 0 );
}
}
{
std::pair<int, short> const tp{ 1, 2 };
{
T1 t1 = construct_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 0 );
}
{
T1 t1 = construct_from_tuple<T1>( std::move(tp) );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 0 );
}
}
{
std::array<int, 3> tp{{ 1, 2, 3 }};
{
T1 t1 = construct_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
{
T1 t1 = construct_from_tuple<T1>( std::move(tp) );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
}
{
std::array<int, 3> const tp{{ 1, 2, 3 }};
{
T1 t1 = construct_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
{
T1 t1 = construct_from_tuple<T1>( std::move(tp) );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
}
{
std::tuple<> tp;
{
T1 t1 = construct_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 0 );
BOOST_TEST_EQ( t1.y, 0 );
BOOST_TEST_EQ( t1.z, 0 );
}
{
T1 t1 = construct_from_tuple<T1>( std::move(tp) );
BOOST_TEST_EQ( t1.x, 0 );
BOOST_TEST_EQ( t1.y, 0 );
BOOST_TEST_EQ( t1.z, 0 );
}
}
{
std::array<int, 0> tp;
{
T1 t1 = construct_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 0 );
BOOST_TEST_EQ( t1.y, 0 );
BOOST_TEST_EQ( t1.z, 0 );
}
{
T1 t1 = construct_from_tuple<T1>( std::move(tp) );
BOOST_TEST_EQ( t1.x, 0 );
BOOST_TEST_EQ( t1.y, 0 );
BOOST_TEST_EQ( t1.z, 0 );
}
}
return boost::report_errors();
}

View File

@@ -0,0 +1,82 @@
// 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>
struct T1
{
int x, y, z;
constexpr T1( int x = 0, int y = 0, int z = 0 ): x(x), y(y), z(z) {}
};
int main()
{
using boost::mp11::construct_from_tuple;
{
constexpr std::tuple<int, short, char> tp{ 1, 2, 3 };
constexpr auto r = construct_from_tuple<T1>( tp );
static_assert( r.x == 1, "r.x == 1" );
static_assert( r.y == 2, "r.y == 2" );
static_assert( r.z == 3, "r.z == 3" );
}
{
constexpr std::pair<short, char> tp{ 1, 2 };
constexpr auto r = construct_from_tuple<T1>( tp );
static_assert( r.x == 1, "r.x == 1" );
static_assert( r.y == 2, "r.y == 2" );
static_assert( r.z == 0, "r.z == 0" );
}
{
constexpr std::array<short, 3> tp{{ 1, 2, 3 }};
constexpr auto r = construct_from_tuple<T1>( tp );
static_assert( r.x == 1, "r.x == 1" );
static_assert( r.y == 2, "r.y == 2" );
static_assert( r.z == 3, "r.z == 3" );
}
#if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 9
// "error: default initialization of an object of const type 'const std::tuple<>' without a user-provided default constructor"
#else
{
constexpr std::tuple<> tp;
constexpr auto r = construct_from_tuple<T1>( tp );
static_assert( r.x == 0, "r.x == 0" );
static_assert( r.y == 0, "r.y == 0" );
static_assert( r.z == 0, "r.z == 0" );
}
#endif
}
#endif

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();
}

72
test/tuple_apply_cx.cpp Normal file
View File

@@ -0,0 +1,72 @@
// 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" );
}
#if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 9
// "error: default initialization of an object of const type 'const std::tuple<>' without a user-provided default constructor"
#else
{
constexpr std::tuple<> tp;
constexpr auto r = boost::mp11::tuple_apply( h, tp );
static_assert( r == 11, "r == 11" );
}
#endif
}
#endif

View File

@@ -7,7 +7,7 @@
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/tuple_for_each.hpp>
#include <boost/mp11/tuple.hpp>
#include <boost/core/lightweight_test.hpp>
#include <tuple>
#include <memory>
@@ -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;

View File

@@ -6,7 +6,7 @@
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/mp11/tuple_for_each.hpp>
#include <boost/mp11/tuple.hpp>
#include <boost/config.hpp>
// Technically std::tuple isn't constexpr enabled in C++11, but it works with libstdc++
@@ -38,6 +38,7 @@ int main()
}
#if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 9
// "error: default initialization of an object of const type 'const std::tuple<>' without a user-provided default constructor"
#else
{