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
{
};
And containing alias typewill return the replaced list of types so that the following example compiles:
using tuple_t = std::tuple<int, char, long, double, float>;
using expected_tuple_t = std::tuple<int, long, double, char, float>;
using result_tuple_t = tuple_shift<1, 3, tuple_t>::type;
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;
};