tuple/type: add prefix query

This commit is contained in:
Danny Robson 2019-12-09 11:22:13 +11:00
parent de282c556e
commit f5afc0130a
2 changed files with 45 additions and 0 deletions

View File

@ -99,5 +99,17 @@ main (void)
tap.expect (std::is_same<dst_t, std::tuple<int>>::value, "tuple type mapping");
}
{
// Test that prefix extraction of a tuple works as expected.
using full_t = std::tuple<i08, i16, i32, i64>;
static constexpr std::size_t LENGTH = 2;
using prefix_t = cruft::tuple::type::prefix_t<LENGTH, full_t>;
using expected_t = std::tuple<i08, i16>;
tap.expect (std::is_same_v<prefix_t, expected_t>, "tuple prefix");
}
return tap.status ();
}

View File

@ -335,4 +335,37 @@ namespace cruft::tuple::type {
//-------------------------------------------------------------------------
template <typename TupleT, std::size_t IndexV>
using nth_t = typename nth<TupleT, IndexV>::type;
///////////////////////////////////////////////////////////////////////////
/// Extract the first CountV types of the tuple-like-object and store them
/// in a tuple definition.
template <
std::size_t CountV,
typename TupleT,
typename Indices = std::make_index_sequence<CountV>
>
struct prefix;
//-------------------------------------------------------------------------
template <
std::size_t CountV,
typename TupleT,
std::size_t ...IndexV
>
struct prefix<
CountV,
TupleT,
std::index_sequence<IndexV...>
> {
using type = std::tuple<
std::tuple_element_t<IndexV, TupleT>...
>;
};
//-------------------------------------------------------------------------
template <std::size_t CountV, typename TupleT>
using prefix_t = typename prefix<CountV, TupleT>::type;
};