15

How can I write an std::array concatenation function?

template <typename T, std::size_t sza, std::size_t szb>
std::array<T, sza+szb> concat (const std::array<T, sza>& aa, 
                                const std::array<T, szb>& ab)
{
    std::array<T, sza+szb> result;
    std::copy(std::begin(aa), std::end(aa), std::begin(result));
    std::copy(std::begin(ab), std::end(ab), std::begin(result) + sza);
    return result;
}

This of course doesn't work when T is not default-constructible. How can this be fixed?

3 Answers 3

12

Convert the parameters to parameter-pack expansions with std::index_sequence and a helper lambda.

template <typename T, std::size_t sza, std::size_t szb>
std::array<T, sza+szb> concat (const std::array<T, sza>& aa, 
                               const std::array<T, szb>& ab)
{
    auto doit = [&]<std::size_t... ai, std::size_t... bi>
        (std::index_sequence<ai...>, std::index_sequence<bi...>)
    {
        return std::array<T, sza+szb>{aa[ai]..., ab[bi]...};
    };
    return doit(std::make_index_sequence<sza>{}, std::make_index_sequence<szb>{});
}
2
12

Explicit template parameter list for lambdas, as shown in n. m.'s answer, were introduced in C++20.

A C++14 solution needs an helper function:

template <typename T, std::size_t... ai, std::size_t... bi>
std::array<T, sizeof...(ai) + sizeof...(bi)>
concat_impl(std::array<T, sizeof...(ai)> const& aa, 
            std::array<T, sizeof...(bi)> const& ab,
            std::index_sequence<ai...>, std::index_sequence<bi...>)
{
    return std::array<T, sizeof...(ai) + sizeof...(bi)>{
      aa[ai]..., ab[bi]...
    };
};

template <typename T, std::size_t sza, std::size_t szb>
std::array<T, sza + szb> concat (std::array<T, sza> const& aa, 
                                 std::array<T, szb> const& ab)
{
    return concat_impl(aa, ab, 
                       std::make_index_sequence<sza>{},
                       std::make_index_sequence<szb>{});
}
6

With the help of std::tuple_cat and std::apply, you can

template <typename T, std::size_t sza, std::size_t szb>
std::array<T, sza+szb> concat(const std::array<T, sza>& aa, 
                              const std::array<T, szb>& ab)
{
  return std::apply([](auto... elems) { return std::array{elems...}; },
                    std::tuple_cat(aa, ab));
}
3
  • 1
    Nice. But I believe std::tuple_cat isn't required to support any "tuple-like" container until c++23 (despite most implantations actually going ahead and supporting them). Feb 6 at 10:03
  • There was no definition of so-called tuple-like until C++23, however, I believe that the standard clearly specified std::tuple_cat's support for std::arrays before C++23.
    – 康桓瑋
    Feb 6 at 10:12
  • It did not. There was an inforormative note, at best, and the preconditions did not give leeway other than making it UB. Feb 6 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.