From 84ef2c111745fcafd32747aeba7a5e83cfede8bd Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 13 Nov 2018 12:56:48 +1100 Subject: [PATCH] tuple/type: add 'nth' accessor for tuples --- test/tuple/type.cpp | 11 +++++++++++ tuple/type.hpp | 34 +++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/test/tuple/type.cpp b/test/tuple/type.cpp index cf295e78..57d2b1d3 100644 --- a/test/tuple/type.cpp +++ b/test/tuple/type.cpp @@ -21,6 +21,17 @@ main (void) tap.expect_eq (cruft::tuple::type::index::value, 1u, "tuple index extraction"); } + { + using tuple_t = std::tuple; + tap.expect ( + std::is_same_v< + cruft::tuple::type::nth_t, + int + >, + "tuple type indexing with 'nth'" + ); + } + { #if !defined(NO_RTTI) auto tuple = std::make_tuple (1u, 1, 1.f, 1.); diff --git a/tuple/type.hpp b/tuple/type.hpp index 6b4a1e9d..517031be 100644 --- a/tuple/type.hpp +++ b/tuple/type.hpp @@ -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 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 struct unique; @@ -309,4 +310,27 @@ namespace cruft::tuple::type { //------------------------------------------------------------------------- template using unique_t = typename unique::type; -} + + + /////////////////////////////////////////////////////////////////////////// + /// Convenience accessor for the 'nth' type of a tuple. + template + struct nth; + + + //------------------------------------------------------------------------- + template + struct nth,0> { + using type = HeadT; + }; + + + //------------------------------------------------------------------------- + template + struct nth,IndexV> : nth,IndexV-1> { }; + + + //------------------------------------------------------------------------- + template + using nth_t = typename nth::type; +};