1
0
forked from boostorg/mp11

Add make_from_tuple

This commit is contained in:
Peter Dimov
2017-06-08 17:42:50 +03:00
parent 359ae25630
commit 08777edbc1
7 changed files with 353 additions and 0 deletions

View File

@@ -592,6 +592,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
<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="#make_from_tuple_t_tp">make_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>
@@ -2822,6 +2823,18 @@ where <code>N</code> is <code>std::tuple_size&lt;typename std::remove_reference&
</div>
</div>
<div class="sect3">
<h4 id="make_from_tuple_t_tp">make_from_tuple&lt;T&gt;(tp)</h4>
<div class="literalblock">
<div class="content">
<pre>template&lt;class T, class Tp&gt; T make_from_tuple(Tp&amp;&amp; tp);</pre>
</div>
</div>
<div class="paragraph">
<p><code>make_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.</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

@@ -20,6 +20,13 @@ http://www.boost.org/LICENSE_1_0.txt
`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.
## make_from_tuple<T>(tp)
template<class T, class Tp> T make_from_tuple(Tp&& tp);
`make_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 C++17.
## tuple_for_each(tp, f)
template<class Tp, class F> constexpr F tuple_for_each(Tp&& tp, F&& f);

View File

@@ -41,6 +41,24 @@ BOOST_CONSTEXPR auto tuple_apply( F && f, Tp && tp )
return detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() );
}
// make_from_tuple
namespace detail
{
template<class T, class Tp, std::size_t... J> BOOST_CONSTEXPR T make_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 make_from_tuple( Tp && tp )
{
return detail::make_from_tuple_impl<T>( std::forward<Tp>(tp), Seq() );
}
// tuple_for_each
namespace detail
{

View File

@@ -2695,6 +2695,24 @@ BOOST_CONSTEXPR auto tuple_apply( F && f, Tp && tp )
return detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() );
}
// make_from_tuple
namespace detail
{
template<class T, class Tp, std::size_t... J> BOOST_CONSTEXPR T make_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 make_from_tuple( Tp && tp )
{
return detail::make_from_tuple_impl<T>( std::forward<Tp>(tp), Seq() );
}
// tuple_for_each
namespace detail
{

View File

@@ -98,6 +98,8 @@ run tuple_for_each.cpp : : : $(REQ) ;
run tuple_for_each_cx.cpp : : : $(REQ) ;
run tuple_apply.cpp : : : $(REQ) ;
run tuple_apply_cx.cpp : : : $(REQ) ;
run make_from_tuple.cpp : : : $(REQ) ;
run make_from_tuple_cx.cpp : : : $(REQ) ;
# set
run mp_set_contains.cpp : : : $(REQ) ;

219
test/make_from_tuple.cpp Normal file
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::make_from_tuple;
{
std::tuple<int, short, char> tp{ 1, 2, 3 };
{
T1 t1 = make_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
{
T1 t1 = make_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 = make_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
{
T1 t1 = make_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 = make_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 = make_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 0 );
}
{
T1 t1 = make_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 = make_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 0 );
}
{
T1 t1 = make_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 = make_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
{
T1 t1 = make_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 = make_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 1 );
BOOST_TEST_EQ( t1.y, 2 );
BOOST_TEST_EQ( t1.z, 3 );
}
{
T1 t1 = make_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 = make_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 0 );
BOOST_TEST_EQ( t1.y, 0 );
BOOST_TEST_EQ( t1.z, 0 );
}
{
T1 t1 = make_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 = make_from_tuple<T1>( tp );
BOOST_TEST_EQ( t1.x, 0 );
BOOST_TEST_EQ( t1.y, 0 );
BOOST_TEST_EQ( t1.z, 0 );
}
{
T1 t1 = make_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,76 @@
// 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::make_from_tuple;
{
constexpr std::tuple<int, short, char> tp{ 1, 2, 3 };
constexpr auto r = make_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 = make_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 = make_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::tuple<> tp;
constexpr auto r = make_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