fork(15) download
  1. #include <iostream>
  2. #include <array>
  3.  
  4. template<unsigned... Is> struct seq{};
  5. template<unsigned N, unsigned... Is>
  6. struct gen_seq : gen_seq<N-1, N-1, Is...>{};
  7. template<unsigned... Is>
  8. struct gen_seq<0, Is...> : seq<Is...>{};
  9.  
  10. template<unsigned N1, unsigned... I1, unsigned N2, unsigned... I2>
  11. // Expansion pack
  12. constexpr std::array<int, N1+N2> concat(const std::array<int, N1>& a1, const std::array<int, N2>& a2, seq<I1...>, seq<I2...>){
  13. return { a1[I1]..., a2[I2]... };
  14. }
  15.  
  16. template<unsigned N1, unsigned N2>
  17. // Initializer for the recursion
  18. constexpr std::array<int, N1+N2> concat(const std::array<int, N1>& a1, const std::array<int, N2>& a2){
  19. return concat(a1, a2, gen_seq<N1>{}, gen_seq<N2>{});
  20. }
  21.  
  22. int main() {
  23. constexpr std::array<int, 3> a1 = {1,2,3};
  24. constexpr std::array<int, 2> a2 = {4,5};
  25.  
  26. constexpr std::array<int,5> res = concat(a1,a2);
  27. for(int i=0; i<res.size(); ++i)
  28. std::cout << res[i] << " "; // 1 2 3 4 5
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1 2 3 4 5