fork(1) download
  1. #include <iostream>
  2.  
  3. template <typename...> struct InterlacePacksHelper;
  4.  
  5. template <template <typename...> class PRes, typename... Ts,
  6. template <typename...> class P, typename U, typename... Us,
  7. typename... Packs>
  8. struct InterlacePacksHelper<PRes<Ts...>, P<U, Us...>, Packs...>
  9. {
  10. using type = typename InterlacePacksHelper<PRes<Ts..., U>, Packs..., P<Us...>>::type;
  11. };
  12.  
  13. template <template <typename...> class PRes, typename... Ts>
  14. struct InterlacePacksHelper<PRes<Ts...>>
  15. {
  16. using type = PRes<Ts...>;
  17. };
  18.  
  19. template <template <typename...> class PRes, typename... Ts,
  20. template <typename...> class P,
  21. typename... Packs>
  22. struct InterlacePacksHelper<PRes<Ts...>, P<>, Packs...>
  23. {
  24. using type = typename InterlacePacksHelper<PRes<Ts...>, Packs...>::type;
  25. };
  26.  
  27.  
  28. // Finally, InterlacePacks itself.
  29. template <typename...> struct InterlacePacks;
  30.  
  31. template <template <typename...> class P, typename... Ts, typename... Packs>
  32. struct InterlacePacks<P<Ts...>, Packs...> : InterlacePacksHelper<P<>, P<Ts...>, Packs...>::type
  33. {
  34. using type = typename InterlacePacksHelper<P<>, P<Ts...>, Packs...>::type;
  35. };
  36.  
  37. // test ----------------------------------------------------------------
  38. template <typename...> struct Pack {};
  39. template <typename...> struct Group {};
  40. template <typename...> struct Wrap {};
  41. struct Object {}; struct Blob {};
  42.  
  43. int main() {
  44. using TestPack1 = Pack<int, double, Object>; // 3 types
  45. using TestPack2 = Group<double, std::string, int, short, long>; // 5 types
  46. using TestPack3 = Wrap<char, short, Blob, std::string>; // 4 types
  47. InterlacePacks<TestPack1, TestPack2, TestPack3>::type interlacedPack;
  48. std::cout << std::boolalpha << std::is_same< decltype(interlacedPack),
  49. Pack<int, double, char, double, std::string, short, Object, int, Blob, short, std::string, long> >::value << std::endl; // true
  50. }
  51.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
true