It's easy to do it with a for loop, but that requires that the element type is default-constructible. Not sure if you can do element-wise construction a la std::vector in constexpr C++20...
Anyway, you can always use index sequences. Please don't use a macro.
I'm pretty sure there's a duplicate question somewhere by the way, with probably more elegant and general answers.
#include <array>
#include <iostream>
#include <utility>
#include <cstddef>
template<typename T, std::size_t... Is, typename... Extra>
constexpr auto extend_impl(std::array<T, sizeof...(Is)> const& base,
std::index_sequence<Is...>,
Extra&&... extra)
{
return std::array{base[Is]..., std::forward<Extra>(extra)...};
}
template<typename T, std::size_t N, typename... Extra>
constexpr auto extend(std::array<T, N> const& base, Extra&&... extra)
{
return extend_impl(base, std::make_index_sequence<N>{},
std::forward<Extra>(extra)...);
}
constexpr std::array base = {1, 2, 3};
constexpr std::array extended = extend(base, 4, 5, 6);
int main()
{
for(auto e : extended)
std::cout << e << " ";
}
On Godbolt
std::arrayinstead you could write one. The simple solution is to just usestd::vector, though it has costs associated with dynamic allocation you may not have to pay here.