How to change types inside type list?

Using std::tuple<>as a list of types, I would like to have a pattern:

template<std::size_t i_src, std::size_t i_dst, class Tuple>
struct tuple_shift
{
    // implementation
};

And containing alias typewill return the replaced list of types so that the following example compiles:

// move type at i_src to i_dst and shift the types
// i_src = 1, i_dst = 3 : right to left shift

using tuple_t          = std::tuple<int, char, long, double, float>; // before
using expected_tuple_t = std::tuple<int, long, double, char, float>; // after

using result_tuple_t = tuple_shift<1, 3, tuple_t>::type; // actual result

static_assert( std::is_same<expected_tuple_t, result_tuple_t>::value, "!" );

Usage example: stable type list type.


Here is my solution, which is in a different galaxy in terms of brevity compared to TC , but this avoids the comparison for each index; it depends on the output of the template to work.

This is actually a partial solution; The complete solution is specialized for the correct extension of the sequence at i_src == i_dstor i_dst < i_src. It works by creating index sequences, and then merging them all into one.

i_src = 1, i_dst = 3, <0, 1, 2, 3, 4>, ( ) :

left_index_seq    = <0>
shifted_index_seq = <2, 3>
right_index_seq   = <4>

:

sequence = <left_index_seq, shifted_index_seq, i_src, right_index_seq>
         = <<0>, <2, 3>, 1, <4>>
         = <0, 2, 3, 1, 4>

#include <tuple>
#include <utility>

template<std::size_t offset, class IndexSequence>
struct index_sequence_offset;

template<std::size_t offset, std::size_t... Is>
struct index_sequence_offset<offset, std::index_sequence<Is...>>
{
    using type = std::index_sequence<( offset + Is )...>;
};

template<std::size_t offset, class IndexSequence>
using make_index_sequence_offset = typename index_sequence_offset
<
    offset, IndexSequence
>::type;

template<class IndexSequence>
struct index_sequence_size;

template<std::size_t... Is>
struct index_sequence_size<std::index_sequence<Is...>>
    : std::integral_constant<std::size_t, sizeof...( Is )>
{};

template<std::size_t i_src, std::size_t i_dst, class Tuple>
struct tuple_shift_indices
{
private:
    template<class LIPack, class SIPack, class RIPack>
    struct tuple_shift_indices_impl;

    template<std::size_t... l_is, std::size_t... s_is, std::size_t... r_is>
    struct tuple_shift_indices_impl
    <
        std::index_sequence<l_is...>,
        std::index_sequence<s_is...>,
        std::index_sequence<r_is...>
    >
    {
        using type = std::index_sequence<l_is..., s_is..., i_src, r_is...>;
    };

public:
    using type = typename tuple_shift_indices_impl
    <
        std::make_index_sequence<i_src>,
        make_index_sequence_offset<i_src + 1, std::make_index_sequence<i_dst - i_src>>,
        make_index_sequence_offset<std::tuple_size<Tuple>::value - 1, std::make_index_sequence<i_dst - i_src - 1>>
    >::type;
};

template<std::size_t i_src, std::size_t i_dst, class Tuple>
struct tuple_shift
{
private:
    template<class IndexSequence>
    struct tuple_shift_impl;

    template<std::size_t... is>
    struct tuple_shift_impl<std::index_sequence<is...>>
    {
        using type = std::tuple<std::tuple_element_t<is, Tuple>...>;
    };

public:
    using type = typename tuple_shift_impl
    <
        typename tuple_shift_indices<i_src, i_dst, Tuple>::type
    >::type;
};
+4
1

constexpr :

constexpr std::size_t old_index(std::size_t new_index, std::size_t src, std::size_t dst) {
    if(new_index == dst) return src;
    if(src < dst && new_index >= src && new_index < dst) return new_index + 1;
    if(src > dst && new_index <= src && new_index > dst) return new_index - 1;
    return new_index;
}

.

template<std::size_t i_src, std::size_t i_dst,
         class Tuple, class = std::make_index_sequence<std::tuple_size_v<Tuple>>>
struct tuple_shift;

template<std::size_t i_src, std::size_t i_dst,
         class Tuple, std::size_t... Is>
struct tuple_shift<i_src, i_dst, Tuple, std::index_sequence<Is...>>
{
    using type = std::tuple<std::tuple_element_t<old_index(Is, i_src, i_dst), Tuple>...>;
};
+6

All Articles