Вставка любого количества типов в пакет аргументов шаблона

InsertTypes<Pack, P<Ts...>, Is...>::type — это Pack с типами Ts..., вставленными в позиции Is... соответственно. Например,

InsertTypes<Pack<int, double, char, long, int>, Pack<short, float, std::string>, 2,4,1>::type,

is

Pack<int, std::string, double, short, char, long, float, int

(short вставляется между double и char, float вставляется между long и int, а std::string вставляется между int и double).

Мой метод: сначала отсортируйте Is... в обратном порядке (от большего к меньшему) и примените Insert для каждого типа в Ts... и каждого int в Is... Зачем сортировать в обратном порядке Is...? Потому что, если они не в таком порядке, вставка типа сдвинет позиции на одну и испортит другие вставки. Но в моем плане есть изъян, который я вскоре объясню. Сначала позвольте мне предоставить написанные мной вспомогательные функции, которые я проверил на корректную работу самостоятельно:

Insert<T, P<Types...>, N>::type — это упаковка P<Types...>, в которой T вставлена ​​в позицию N.

template <typename, typename, typename, int> struct InsertHelper;

template <typename T, template <typename...> class P, typename First, typename... Rest, typename... Accumulated>
struct InsertHelper<T, P<First, Rest...>, P<Accumulated...>, 0> {
    using type = P<Accumulated..., T, First, Rest...>;
};

template <typename T, template <typename...> class P, typename First, typename... Rest, typename... Accumulated, int N>
struct InsertHelper<T, P<First, Rest...>, P<Accumulated...>, N> : InsertHelper<T, P<Rest...>, P<Accumulated..., First>, N-1> {};

template <typename, typename, int> struct Insert;

template <typename T, template <typename...> class P, typename... Types, int N>
struct Insert<T, P<Types...>, N> : InsertHelper<T, P<Types...>, P<>, N> {};

Теперь ReverseSortIntSequence (с использованием быстрой сортировки):

template <int, typename> struct PrependInt;

template <int N, template <int...> class Z, int... Is>  
struct PrependInt<N, Z<Is...>> {  
    using type = Z<N, Is...>;  
};

template <template<int> class, typename> struct FilterInts;  

template <template<int> class F, template <int...> class Z, int I, int... Is>  
struct FilterInts<F, Z<I, Is...>> {
    using type = typename std::conditional<F<I>::value,
        typename PrependInt<I, typename FilterInts<F, Z<Is...>>::type>::type,
        typename FilterInts<F, Z<Is...>>::type
    >::type;  
};

template <template<int> class F, template <int...> class Z>  
struct FilterInts<F, Z<>> {  
    using type = Z<>;  
};  

template <typename, typename> struct MergeIntSequences;  

template <template <int...> class Z, int... Is, int... Js>  
struct MergeIntSequences<Z<Is...>, Z<Js...>> {  
    using type = Z<Is..., Js...>;
};

template <typename> struct ReverseSortIntSequence;  

template <template <int...> class Z, int N, int... Is>  
struct ReverseSortIntSequence<Z<N, Is...>> {  
    template<int I> struct less_than : std::integral_constant<bool, (I >= N)> {};
    template <int I> struct more_than : std::integral_constant<bool, (I < N)> {};  
    using subsequence_less_than_N = typename FilterInts<less_than, Z<Is...>>::type;
    using subsequence_more_than_N = typename FilterInts<more_than, Z<Is...>>::type; 
    using type = typename MergeIntSequences<typename ReverseSortIntSequence<subsequence_less_than_N>::type,  
        typename PrependInt<N, typename ReverseSortIntSequence<subsequence_more_than_N>::type>::type 
    >::type;
};

template<template <int...> class Z>  
struct ReverseSortIntSequence<Z<>> {  
    using type = Z<>;  
};

Теперь сам InsertTypes:

template <typename, typename, typename> struct InsertTypesHelper;

template <typename Pack, template <typename...> class P, template <int...> class Z>
struct InsertTypesHelper<Pack, P<>, Z<>> {
    using type = Pack;
};

template <typename Pack, template <typename...> class P, typename First, typename... Rest, template <int...> class Z, int N, int... Ns>
struct InsertTypesHelper<Pack, P<First, Rest...>, Z<N, Ns...>> : InsertTypesHelper<typename Insert<First, Pack, N>::type, P<Rest...>, Z<Ns...>> {};

template <typename, typename, int...> struct InsertTypes;

template <typename Pack, template <typename...> class P, typename... Types, int... Is>
struct InsertTypes<Pack, P<Types...>, Is...> : InsertTypesHelper<Pack, P<Types...>, typename ReverseSortIntSequence<index_sequence<Is...>>::type> {};

Теперь мои тесты:

int main() {
    std::cout << std::is_same<
        typename ReverseSortIntSequence<index_sequence<5,10,8,4,0,2,1,2,7,8,3>>::type,
        index_sequence<10,8,8,7,5,4,3,2,2,1,0>
    >::value << std::endl;  // true

    std::cout << std::is_same<
        InsertTypesHelper<Pack<int, double, char, long, int>, Pack<float, short, std::string>, index_sequence<4,2,1>>::type,
        Pack<int, std::string, double, short, char, long, float, int>
    >::value << std::endl;  // true (*)

    std::cout << std::is_same<
        typename ReverseSortIntSequence<index_sequence<2,4,1>>::type,
        index_sequence<4,2,1>
    >::value << std::endl;  // true (**)

    std::cout << std::is_same<
        InsertTypes<Pack<int, double, char, long, int>, Pack<short, float, std::string>, 2,4,1>::type,
        Pack<int, std::string, double, short, char, long, float, int>
    >::value << std::endl;  // false (rats!)
}

Я получаю false выше, потому что, несмотря на то, что (*) и (**) верны выше, мы должны переставить Pack<short, float, std::string> таким же образом, как переставлены 2,4,1, чтобы получить это в обратном порядке сортировки. Я мог бы продолжить это исправление, но теперь оно становится за бортом. Я все равно продолжу это, но я серьезно подозреваю, что есть лучший метод, возможно, тоже довольно короткий.

Любые хорошие идеи здесь? Я подумал об извлечении пар типов, определяемых индексами (вставленные типы будут располагаться между парами), но это не сработает, если в исходном пакете есть повторяющиеся типы (а также из-за вставляемых типов).

Обновление: я закончил хелпер по перестановке, рассмотренный выше, и теперь все работает правильно. Но должно быть лучшее и более короткое решение, чем весь этот бардак.

template <int, typename> struct NthType;

template <int N, template <typename...> class P, typename First, typename... Rest>
struct NthType<N, P<First, Rest...>> : NthType<N-1, P<Rest...>> {};

template <template <typename...> class P, typename First, typename... Rest>
struct NthType<0, P<First, Rest...>> {
    using type = First;
};

template <int, int, typename> struct FindIndexOfIntHelper;

template <int N, int FindMe, template <int...> class Z, int... Rest>
struct FindIndexOfIntHelper<N, FindMe, Z<FindMe, Rest...>> : std::integral_constant<int, N> {};

template <int N, int FindMe, template <int...> class Z>
struct FindIndexOfIntHelper<N, FindMe, Z<>> : std::integral_constant<int, -1> {};  // Not found.

template <int N, int FindMe, template <int...> class Z, int First, int... Rest>
struct FindIndexOfIntHelper<N, FindMe, Z<First, Rest...>> : FindIndexOfIntHelper<N+1, FindMe, Z<Rest...>> {};

template <int FindMe, typename Pack>
using FindIndexOfInt = FindIndexOfIntHelper<0, FindMe, Pack>;

template <typename, typename, typename, typename> struct PermutePackHelper;

template <typename Pack, template <typename...> class P, typename... Accumulated, typename IndicesPack, template <int...> class Z>
struct PermutePackHelper<Pack, P<Accumulated...>, IndicesPack, Z<>> {
    using type = P<Accumulated...>;
};

template <typename Pack, template <typename...> class P, typename... Accumulated, typename IndicesPack, template <int...> class Z, int I, int... Is>
struct PermutePackHelper<Pack, P<Accumulated...>, IndicesPack, Z<I, Is...>> :
    PermutePackHelper<Pack, P<Accumulated..., typename NthType<FindIndexOfInt<I, IndicesPack>::value, Pack>::type>,
        IndicesPack, Z<Is...>> {};

template <typename, typename, typename> struct PermutePack;

template <template <typename...> class P, typename... Types, template <int...> class Z, int... Is, int... Js>
struct PermutePack<P<Types...>, Z<Is...>, Z<Js...>> : PermutePackHelper<P<Types...>, P<>, Z<Is...>, Z<Js...>> {};

person prestokeys    schedule 27.02.2015    source источник
comment
Вероятно, было бы проще, если бы вы требовали, чтобы список позиций был в порядке возрастания. т.е. 1, 2, 4 вместо 2, 4, 1.   -  person jrok    schedule 27.02.2015
comment
Рассматривали ли вы вместо сортировки индексов просто добавление 1 к тем, которые больше текущего индекса, при передаче их следующему экземпляру?   -  person Dark Falcon    schedule 27.02.2015
comment
@jrok. Тогда было бы слишком просто: перевернуть индексы, а затем перевернуть пачку вставленных типов. Задача, которой я наслаждаюсь, состоит в том, чтобы не ограничивать пользовательский ввод. К Dark Falcon мы добавляем 1 только в том случае, если индексы возрастают. Если они идут вверх и вниз, вверх и вниз и т. д., то это уже не так просто.   -  person prestokeys    schedule 27.02.2015
comment
Я не понимаю, почему выполнение этого от большего к меньшему дает правильный ответ. После этого тип, связанный с 4, не находится на 4-м месте в списке типов. Я предполагаю, что это после того, как был 4-й элемент в исходном списке? Что делать, если в точке 2 вставлено несколько типов? Нет четкого правильного ответа, есть только произвольный, что является признаком того, что ваш дизайн несовершенен.   -  person Yakk - Adam Nevraumont    schedule 28.02.2015
comment
Мысль: сопоставить types<Ts...> с types<types<Ts>...>. Затем вставьте, не беспокоясь о ударах. Затем расплющите.   -  person Yakk - Adam Nevraumont    schedule 28.02.2015
comment
Да! Это замечательная идея! Я займусь этим сейчас!   -  person prestokeys    schedule 28.02.2015


Ответы (2)


Есть способ сделать это с помощью boost::mpl, который, я думаю, немного легче усвоить(?) Я добавил много комментариев, чтобы объяснить шаги, и соответствующие отступы, чтобы сделать код немного понятнее... может быть, это поможет ...

#include <iostream>
#include <type_traits>

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/sort.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/insert.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/advance.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/transform.hpp>

using namespace boost::mpl;

template <typename R, typename It, typename End>
struct type_inserter
{ 
  // index to insert
  typedef typename deref<It>::type::first KType;
  // type to insert
  typedef typename deref<It>::type::second VType;
  // recurse
  typedef typename type_inserter<
    typename insert<
      R,
      // calculate the location based on the index
      typename advance<typename begin<R>::type, KType>::type,
      VType
    >::type,
    typename next<It>::type,
    End
  >::type type;
};

template <typename R, typename End>
struct type_inserter<R, End, End>
{
  typedef R type;
};

template <typename P>
struct index_access
{
  typedef typename P::first type;
};

struct make_pair
{
  template<typename Kv, typename Pv>
  struct apply
  {
    typedef pair<Kv, Pv> type;
  };
};

template <typename P1, typename P2, int ...I>
struct insert_pack
{
  // transform P2 and indexes
  static_assert(sizeof...(I) == size<P2>::value, "indexes/count P2 mismatch");
  // this iterates through both sequences, constructing a pair and inserting into a vector
  typedef typename transform<vector_c<int, I...>, P2, make_pair, back_inserter<vector0<>>>::type entries;
  // sort the sequence by the index..
  typedef typename sort<entries, greater<index_access<_1>, index_access<_2>>>::type reversed;
  // once sorted, insert the sorted range into the main vector using custom inserter
  typedef typename type_inserter<P1, typename begin<reversed>::type, typename end<reversed>::type>::type type;
};

int main()
{
  typedef vector<int, double, char, long, int> pack1;
  typedef vector<short, float, std::string> pack2;
  // this combines the pack
  typedef insert_pack<pack1, pack2, 2, 4, 1>::type combined;
  // this is what we expect
  typedef vector<int, std::string, double, short, char, long, float, int> packs;
  // sanity check
  BOOST_MPL_ASSERT(( equal< packs, combined> ));
}
person Nim    schedule 28.02.2015
comment
Еще не знаком с Boost, но когда я запустил ваш код с typedef insert_pack<pack1, pack2, 2, 2, 2>::type combined;, я получил результат vector<int, double, std::string, float, short, char, long, int>, что и дает моя программа (все типы вставлены в позицию 2, что заставляет их отображаться в обратном порядке). Однако dyp выше утверждает, что стабильная вставка должна появляться в необратимом порядке. Это просто дело вкуса, я полагаю? - person prestokeys; 01.03.2015
comment
Я не знаю, согласен ли я с этим, последовательность 2, 2, 2 указывает, что каждая запись в процессе обработки должна быть вставлена ​​в позицию 2 с состоянием последовательности на тот момент времени, я не понимаю, как это означает вставить этот диапазон в позицию 2 (чтобы сохранить порядок), я думаю, чтобы сохранить, вам нужно поддерживать вставку диапазона - это другая (но более простая проблема..) - person Nim; 02.03.2015
comment
... Если вы взяли стандартный вектор и вставили три записи в позицию 2, вы столкнетесь с аналогичной обратной последовательностью, это фактически ожидаемое поведение операции вставки (в этой точке она работает автономно с последовательностью..) Я не знаю не знаю, делает ли это яснее.. - person Nim; 02.03.2015

Это благодаря быстрому предложению Yakk выше. Теперь у нас есть красивое элегантное решение!

#include <iostream>
#include <string>

template <typename, typename, typename, int> struct InsertIntoPackOfPacksHelper;

template <typename T, template <typename...> class P, typename First, typename... Rest, typename... Accumulated>
struct InsertIntoPackOfPacksHelper<T, P<P<First>, Rest...>, P<Accumulated...>, 0> {
    using type = P<Accumulated..., P<T, First>, Rest...>;
};

template <typename T, template <typename...> class P, typename First, typename... Rest, typename... Accumulated, int N>
struct InsertIntoPackOfPacksHelper<T, P<First, Rest...>, P<Accumulated...>, N> : InsertIntoPackOfPacksHelper<T, P<Rest...>, P<Accumulated..., First>, N-1> {};

template <typename, typename, int> struct InsertIntoPackOfPacks;

template <typename T, template <typename...> class P, typename... Types, int N>
struct InsertIntoPackOfPacks<T, P<Types...>, N> : InsertIntoPackOfPacksHelper<T, P<Types...>, P<>, N> {};

template <typename, typename>
struct FlattenPackHelper;

template <template <typename...> class P, template <typename...> class P2, typename... Ts, typename... Us, typename... Rest>
struct FlattenPackHelper<P<Ts...>, P<P2<Us...>, Rest...>> : FlattenPackHelper<P<Ts...>, P<Us..., Rest...>> {};

template <template <typename...> class P, typename... Ts, typename First, typename... Rest>
struct FlattenPackHelper<P<Ts...>, P<First, Rest...>> : FlattenPackHelper<P<Ts..., First>, P<Rest...>> {};

template <template <typename...> class P, typename... Ts, typename... Rest>
struct FlattenPackHelper<P<Ts...>, P<std::string, Rest...>> : FlattenPackHelper<P<Ts..., std::string>, P<Rest...>> {};

template <template <typename...> class P, typename... Ts>
struct FlattenPackHelper<P<Ts...>, P<>> {
    using type = P<Ts...>;
};

template <typename T> struct FlattenPack;

template <template <typename...> class P, typename... Ts>
struct FlattenPack<P<Ts...>> : FlattenPackHelper<P<>, P<Ts...>> {};

template <typename, typename, typename> struct InsertTypesHelper;

template <typename PackOfPacks, template <typename...> class P, template <int...> class Z>
struct InsertTypesHelper<PackOfPacks, P<>, Z<>> {
    using type = PackOfPacks;
};

template <typename PackOfPacks, template <typename...> class P, typename First, typename... Rest, template <int...> class Z, int I, int... Is>
struct InsertTypesHelper<PackOfPacks, P<First, Rest...>, Z<I, Is...>> :
    InsertTypesHelper<typename InsertIntoPackOfPacks<First, PackOfPacks, I>::type, P<Rest...>, Z<Is...>> {};

template <typename, typename, int...> struct InsertTypes;

template <int...> struct index_sequence {};

template <template <typename...> class P, typename... Types, typename... Ts, int... Is>
struct InsertTypes<P<Types...>, P<Ts...>, Is...> {
    using type = typename FlattenPack<typename InsertTypesHelper<P<P<Types>...>, P<Ts...>, index_sequence<Is...>>::type>::type;
};

// Testing
template <typename...> struct Pack {};

int main() {
    std::cout << std::boolalpha << std::is_same<
        InsertTypes<Pack<int, double, char, long, int>, Pack<short, float, std::string>, 2,4,1>::type,
        Pack<int, std::string, double, short, char, long, float, int>
    >::value << std::endl;  // true
}

И для чего это стоит, вот сравнение с моим оригинальным, ужасно длинным решением, изложенным в моем первоначальном вопросе:

#include <iostream>
#include <string>

// ReverseSortIntSequence (using quicksort):
template <int, typename> struct PrependInt;

template <int N, template <int...> class Z, int... Is>  
struct PrependInt<N, Z<Is...>> {  
    using type = Z<N, Is...>;  
};

template <template<int> class, typename> struct FilterInts;  

template <template<int> class F, template <int...> class Z, int I, int... Is>  
struct FilterInts<F, Z<I, Is...>> {
    using type = typename std::conditional<F<I>::value,
        typename PrependInt<I, typename FilterInts<F, Z<Is...>>::type>::type,
        typename FilterInts<F, Z<Is...>>::type
    >::type;  
};

template <template<int> class F, template <int...> class Z>  
struct FilterInts<F, Z<>> {  
    using type = Z<>;  
};  

template <typename, typename> struct MergeIntSequences;  

template <template <int...> class Z, int... Is, int... Js>  
struct MergeIntSequences<Z<Is...>, Z<Js...>> {  
    using type = Z<Is..., Js...>;
};

template <int I, int J>
struct IntLessThan : std::conditional<(I < J), std::true_type, std::false_type>::type {};

template <typename, template <int, int> class = IntLessThan> struct SortIntSequence;  

template <template <int...> class Z, int N, int... Is, template <int, int> class Comparator>  
struct SortIntSequence<Z<N, Is...>, Comparator> {  
    template<int I> struct less_than : std::integral_constant<bool, Comparator<I,N>::value> {};
    template <int I> struct more_than : std::integral_constant<bool, Comparator<N,I>::value || I == N> {};  
    using subsequence_less_than_N = typename FilterInts<less_than, Z<Is...>>::type;
    using subsequence_more_than_N = typename FilterInts<more_than, Z<Is...>>::type; 
    using type = typename MergeIntSequences<typename SortIntSequence<subsequence_less_than_N, Comparator>::type,  
        typename PrependInt<N, typename SortIntSequence<subsequence_more_than_N, Comparator>::type>::type 
    >::type;
};

template<template <int...> class Z, template <int, int> class Comparator>  
struct SortIntSequence<Z<>, Comparator> {  
    using type = Z<>;  
};

// Given Pack, Z<Is...>, Z<Js...>, rearrange Pack in the same way that Z<Is...> is rearranged to get Z<Js...>.  PermutePack will take care of this.
template <int, typename> struct NthType;

template <int N, template <typename...> class P, typename First, typename... Rest>
struct NthType<N, P<First, Rest...>> : NthType<N-1, P<Rest...>> {};

template <template <typename...> class P, typename First, typename... Rest>
struct NthType<0, P<First, Rest...>> {
    using type = First;
};

template <int, int, typename> struct FindIndexOfIntHelper;

template <int N, int FindMe, template <int...> class Z, int... Rest>
struct FindIndexOfIntHelper<N, FindMe, Z<FindMe, Rest...>> : std::integral_constant<int, N> {};

template <int N, int FindMe, template <int...> class Z>
struct FindIndexOfIntHelper<N, FindMe, Z<>> : std::integral_constant<int, -1> {};  // Not found.

template <int N, int FindMe, template <int...> class Z, int First, int... Rest>
struct FindIndexOfIntHelper<N, FindMe, Z<First, Rest...>> : FindIndexOfIntHelper<N+1, FindMe, Z<Rest...>> {};

template <int FindMe, typename Pack>
using FindIndexOfInt = FindIndexOfIntHelper<0, FindMe, Pack>;

template <typename, typename, typename, typename> struct PermutePackHelper;

template <typename Pack, template <typename...> class P, typename... Accumulated, typename IndicesPack, template <int...> class Z>
struct PermutePackHelper<Pack, P<Accumulated...>, IndicesPack, Z<>> {
    using type = P<Accumulated...>;
};

template <typename Pack, template <typename...> class P, typename... Accumulated, typename IndicesPack, template <int...> class Z, int I, int... Is>
struct PermutePackHelper<Pack, P<Accumulated...>, IndicesPack, Z<I, Is...>> :
    PermutePackHelper<Pack, P<Accumulated..., typename NthType<FindIndexOfInt<I, IndicesPack>::value, Pack>::type>,
        IndicesPack, Z<Is...>> {};

template <typename, typename, typename> struct PermutePack;

template <template <typename...> class P, typename... Types, template <int...> class Z, int... Is, int... Js>
struct PermutePack<P<Types...>, Z<Is...>, Z<Js...>> : PermutePackHelper<P<Types...>, P<>, Z<Is...>, Z<Js...>> {};

// Now InsertTypes itself.
template <typename, typename, typename, int> struct InsertHelper;

template <typename T, template <typename...> class P, typename First, typename... Rest, typename... Accumulated>
struct InsertHelper<T, P<First, Rest...>, P<Accumulated...>, 0> {
    using type = P<Accumulated..., T, First, Rest...>;
};

template <typename T, template <typename...> class P, typename First, typename... Rest, typename... Accumulated, int N>
struct InsertHelper<T, P<First, Rest...>, P<Accumulated...>, N> : InsertHelper<T, P<Rest...>, P<Accumulated..., First>, N-1> {};

template <typename, typename, int> struct Insert;

template <typename T, template <typename...> class P, typename... Types, int N>
struct Insert<T, P<Types...>, N> : InsertHelper<T, P<Types...>, P<>, N> {};

template <typename, typename, typename> struct InsertTypesHelper;

template <typename Pack, template <typename...> class P, template <int...> class Z>
struct InsertTypesHelper<Pack, P<>, Z<>> {
    using type = Pack;
};

template <typename Pack, template <typename...> class P, typename First, typename... Rest, template <int...> class Z, int N, int... Ns>
struct InsertTypesHelper<Pack, P<First, Rest...>, Z<N, Ns...>> : InsertTypesHelper<typename Insert<First, Pack, N>::type, P<Rest...>, Z<Ns...>> {};

template <typename, typename, int...> struct InsertTypes;

template <typename Pack1, typename Pack2, typename Sequence, typename ReverseSequence>
using InsertTypesHelperAlias = InsertTypesHelper<Pack1, typename PermutePack<Pack2, Sequence, ReverseSequence>::type, ReverseSequence>;

template <int I, int J>
struct IntGreaterThan : std::conditional<(I > J), std::true_type, std::false_type>::type {};

template <int...> struct index_sequence {};

template <typename Pack, template <typename...> class P, typename... Types, int... Is>
struct InsertTypes<Pack, P<Types...>, Is...> : InsertTypesHelperAlias<Pack, P<Types...>, index_sequence<Is...>, typename SortIntSequence<index_sequence<Is...>, IntGreaterThan>::type> {};  // SortIntSequence<index_sequence<Is...>, IntGreaterThan>::type is the reverse sort of index_sequence<Is...>.

// Testing
template <typename...> struct Pack {};

int main() {
    std::cout << std::boolalpha << std::is_same<
        InsertTypes<Pack<int, double, char, long, int>, Pack<short, float, std::string>, 2,4,1>::type,
        Pack<int, std::string, double, short, char, long, float, int>
    >::value << std::endl;  // true 
}
person prestokeys    schedule 27.02.2015
comment
Если вы запишите вставку (первую частичную специализацию) как template <typename T, template <typename...> class P, typename... First, typename... Rest, typename... Accumulated> struct InsertIntoPackOfPacksHelper<T, P<P<First...>, Rest...>, P<Accumulated...>, 0> { using type = P<Accumulated..., P<First..., T>, Rest...>; };, то вы также можете поддерживать стабильную вставку по тому же индексу. - person dyp; 28.02.2015
comment
Да. Вчера я сделал еще одно изменение, так что InsertTypes<Pack<int, double, char, long, int>, Pack<short, float, bool, std::string, int>, 2,4,2,1,2>::type дает Pack<int, std::string, double, int, bool, short, char, long, float, int>, но я не уверен, что это должен быть результат, потому что это сбивает с толку, что должны означать множественные вставки в один и тот же индекс. Моя частичная специализация была struct InsertIntoPackOfPacksHelper<T, P<P<First, Rest...>, Others...>, P<Accumulated...>, 0> { using type = P<Accumulated..., P<T, First, Rest...>, Others...>; }; - person prestokeys; 28.02.2015
comment
Я не совсем понимаю, зачем нужен First в вашей частичной специализации. Для нескольких вставок по одному и тому же индексу, как я уже сказал, я бы рекомендовал стабильность, то есть Pack<int, string, double, short, int, char, long, float, int> (Подсказка: использование типов массивов для примеров может улучшить читаемость, например, InsertTypes< Pack<orig[0], orig[1], orig[2]>, Pack<insert[0], insert[1], insert[2]>, 2,1,3 > с struct orig {}; struct insert {};) - person dyp; 28.02.2015