What is the best way of implementing index-based insertion and deletion of a type in a variadic template type list (parameter pack)?
Desired code/behavior:
template<typename...> struct List { /* ... */ };
static_assert(is_same
<
List<int, char, float>::Insert<int, 0>,
List<int, int, char, float>
>());
static_assert(is_same
<
List<int, char, float>::Insert<int, 2>,
List<int, char, int, float>
>());
static_assert(is_same
<
List<int, char, float>::Remove<0>,
List<char, float>
>());
static_assert(is_same
<
List<int, char, float>::Remove<1>,
List<int, float>
>());
I tried an implementation based on pushing back the arguments in an initially-empty list, but it was very hard to read/maintain. The parameters were similar to this:
template<typename T, int I, int ITarget, typename TResult> struct InsertImpl;
I constantly increment I
until it equals ITarget
, pushing back existing types in TResult
, which is a List<...>
. When I
equals ITarget
, I push back T
in TResult
as well.
Removing a type had a similar implementation - instead of pushing back twice when the indices were equal, I simply skipped the type.
My cumbersome solution would implement insertion and removal in terms of pushing and popping. I believe it would be more elegant to have pushing to the front equal to Insert<0>
and pushing to the back equal to Insert<size>
. The same applies for popping from the front and from the back.
Is there a better way of doing this? Could C++14 features help?
Not sure there is any "best" way, but this is a non-recursive way:
#include <utility>
#include <type_traits>
#include <tuple>
template<typename...Ts> struct List;
template<typename T> struct ListFromTupleImpl;
template<typename...Ts>
struct ListFromTupleImpl<std::tuple<Ts...>>
{ using type = List<Ts...>; };
template<typename T>
using ListFromTuple = typename ListFromTupleImpl<T>::type;
template<typename...Ts>
using TupleCat = decltype(std::tuple_cat(std::declval<Ts>()...));
template<typename...Ts>
using ListFromTupleCat = ListFromTuple<TupleCat<Ts...>>;
template<unsigned P,typename T,typename I> struct RemoveFromListImpl;
template<unsigned P,typename...Ts,std::size_t...Is>
struct RemoveFromListImpl<P,List<Ts...>,std::index_sequence<Is...>>
{
using type = ListFromTupleCat<
std::conditional_t<(Is==P),std::tuple<>,std::tuple<Ts>>...>;
};
// All elements < P
template<unsigned P,typename T,typename I> struct HeadImpl;
template<unsigned P,typename...Ts,std::size_t...Is>
struct HeadImpl<P,List<Ts...>,std::index_sequence<Is...>>
{
using type = TupleCat<
std::conditional_t<(Is>=P),std::tuple<>,std::tuple<Ts>>...>;
};
// All elements >= P
template<unsigned P,typename T,typename I> struct TailImpl;
template<unsigned P,typename...Ts,std::size_t...Is>
struct TailImpl<P,List<Ts...>,std::index_sequence<Is...>>
{
using type = TupleCat<
std::conditional_t<(Is<P),std::tuple<>,std::tuple<Ts>>...>;
};
template<typename N,unsigned P,typename T,typename I>
struct InsertIntoListImpl
{
using head = typename HeadImpl<P,T,I>::type;
using tail = typename TailImpl<P,T,I>::type;
using type = ListFromTupleCat<head,std::tuple<N>,tail>;
};
template<typename...Ts> struct List {
/* ... */
template<std::size_t P>
using Remove =
typename RemoveFromListImpl<P,List<Ts...>,
std::index_sequence_for<Ts...>>::type;
template<typename N,std::size_t P>
using Insert =
typename InsertIntoListImpl<N,P,List<Ts...>,
std::index_sequence_for<Ts...>>::type;
};
static_assert(std::is_same
<
List<int, char, float>::Remove<0>,
List<char, float>
>(), "");
static_assert(std::is_same
<
List<int, char, float>::Remove<1>,
List<int, float>
>(), "");
static_assert(std::is_same
<
List<int, char, float>::Insert<int, 0>,
List<int, int, char, float>
>(), "");
static_assert(std::is_same
<
List<int, char, float>::Insert<int, 2>,
List<int, char, int, float>
>(), "");
int main(){}
Live example
Since you mentioned C++14, here's another one making use of std::index_sequence
. The main reason for which I think the solution is worth mentioning is the use of constexpr
mapping functions to place the types in their positions in the resulting List
. This makes the implementation relatively straightforward.
#include <cstddef>
#include <tuple>
#include <utility>
template<typename...> struct List;
constexpr std::size_t map_ins(std::size_t i, std::size_t from, std::size_t to)
{
return i < to ? i : i == to ? from : i - 1;
}
template<typename, typename, std::size_t, typename...> struct ins_hlp;
template<std::size_t... Is, typename U, std::size_t N, typename... Ts>
struct ins_hlp<std::index_sequence<Is...>, U, N, Ts...>
{
static_assert(N <= sizeof...(Ts), "Insert index out of range");
using type = List<std::tuple_element_t<map_ins(Is, sizeof...(Ts), N), std::tuple<Ts..., U>>...>;
};
constexpr std::size_t map_rem(std::size_t i, std::size_t idx)
{
return i < idx ? i : i + 1;
}
template<typename, std::size_t, typename...> struct rem_hlp_2;
template<std::size_t... Is, std::size_t N, typename... Ts>
struct rem_hlp_2<std::index_sequence<Is...>, N, Ts...>
{
using type = List<std::tuple_element_t<map_rem(Is, N), std::tuple<Ts...>>...>;
};
template<std::size_t N, typename... Ts> struct rem_hlp
{
static_assert(N < sizeof...(Ts), "Remove index out of range");
using type = typename rem_hlp_2<std::make_index_sequence<sizeof...(Ts) - 1>, N, Ts...>::type;
};
template<typename... Ts> struct List
{
template<typename U, std::size_t N> using Insert = typename ins_hlp<std::make_index_sequence<sizeof...(Ts) + 1>, U, N, Ts...>::type;
template<std::size_t N> using Remove = typename rem_hlp<N, Ts...>::type;
};
Sorry for the long lines, but I didn't find another meaningful way to format those argument lists.
The only reason for having an additional helper for Remove
is bounds checking; if that's not needed, Remove
can use the same pattern as Insert
.
Using Eric Niebler's Tiny Meta-Programming Library (DEMO):
template <std::size_t N, typename List>
using take_c =
meta::reverse<
meta::drop_c<
meta::size<List>::value - N,
meta::reverse<List>
>>;
template <typename...Ts> struct List {
using mlist = meta::list<Ts...>;
template <typename T, std::size_t I>
using Insert =
meta::apply_list<
meta::quote<::List>,
meta::concat<
take_c<I, mlist>,
meta::list<T>,
meta::drop_c<I, mlist>
>>;
template <std::size_t I>
using Remove =
meta::apply_list<
meta::quote<::List>,
meta::concat<
take_c<I, mlist>,
meta::drop_c<I + 1, mlist>
>>;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With