tuple/type: add 'nth' accessor for tuples

This commit is contained in:
Danny Robson 2018-11-13 12:56:48 +11:00
parent 8c0c374a53
commit 84ef2c1117
2 changed files with 40 additions and 5 deletions

View File

@ -21,6 +21,17 @@ main (void)
tap.expect_eq (cruft::tuple::type::index<tuple_t,int>::value, 1u, "tuple index extraction");
}
{
using tuple_t = std::tuple<float,int,void>;
tap.expect (
std::is_same_v<
cruft::tuple::type::nth_t<tuple_t,1>,
int
>,
"tuple type indexing with 'nth'"
);
}
{
#if !defined(NO_RTTI)
auto tuple = std::make_tuple (1u, 1, 1.f, 1.);

View File

@ -172,8 +172,8 @@ namespace cruft::tuple::type {
///////////////////////////////////////////////////////////////////////////
// transform a tuple-like type (ie, something that responds to
// tuple_element) into a tuple
/// Transform a tuple-like type (ie, something that responds to
/// tuple_element) into a tuple
template <
typename TupleT,
typename Indices = std::make_index_sequence<
@ -202,7 +202,7 @@ namespace cruft::tuple::type {
///////////////////////////////////////////////////////////////////////////
// concatenate a variadic collection of tuple-like types
/// Concatenate a variadic collection of tuple-like types
template <typename ...Args>
struct cat;
@ -234,7 +234,7 @@ namespace cruft::tuple::type {
///////////////////////////////////////////////////////////////////////////
// remove a type from a tuple-like type
/// Remove a type from a tuple-like type
template <
typename DoomedT,
typename TupleT
@ -280,6 +280,7 @@ namespace cruft::tuple::type {
///////////////////////////////////////////////////////////////////////////
/// Remove duplicate types from the list of held types in a tuple
template <typename TupleT>
struct unique;
@ -309,4 +310,27 @@ namespace cruft::tuple::type {
//-------------------------------------------------------------------------
template <typename T>
using unique_t = typename unique<T>::type;
}
///////////////////////////////////////////////////////////////////////////
/// Convenience accessor for the 'nth' type of a tuple.
template <typename TupleT, std::size_t IndexV>
struct nth;
//-------------------------------------------------------------------------
template <typename HeadT, typename ...TailT>
struct nth<std::tuple<HeadT,TailT...>,0> {
using type = HeadT;
};
//-------------------------------------------------------------------------
template <typename HeadT, typename ...TailT, std::size_t IndexV>
struct nth<std::tuple<HeadT,TailT...>,IndexV> : nth<std::tuple<TailT...>,IndexV-1> { };
//-------------------------------------------------------------------------
template <typename TupleT, std::size_t IndexV>
using nth_t = typename nth<TupleT, IndexV>::type;
};