在编译时将两个或多个不同大小的数组合并为一个数组

Mar*_*tus 6 c++ arrays c++14

我无法找到有关如何在现代 C++ 中在编译时组合两个或多个数组的答案。

#include <array>
#include <cstdint>

const std::array<std::uint8_t, 1> one_elem = {1};
const std::array<std::uint8_t, 2> two_elem = {2, 3};
const std::array<std::uint8_t, 3> all = {one_elem, two_elem}; 
// expected: all == {1, 2, 3}
Run Code Online (Sandbox Code Playgroud)

我会很高兴任何易于阅读的东西,例如

std::uint8_t one_elem[] = {1};
std::uint8_t two_elem[] = {2, 3};
std::uint8_t all[] = {one_elem, two_elem}; // cannot be that hard
Run Code Online (Sandbox Code Playgroud)

有办法吗?我能做些什么来解决这个问题?

Jus*_*tin 6

如果您使用的是 C++17,您可以这样做:

template <typename T, std::size_t N1, std::size_t N2>
constexpr std::array<T, N1 + N2> concat(std::array<T, N1> lhs, std::array<T, N2> rhs)
{
    std::array<T, N1 + N2> result{};
    std::size_t index = 0;

    for (auto& el : lhs) {
        result[index] = std::move(el);
        ++index;
    }
    for (auto& el : rhs) {
        result[index] = std::move(el);
        ++index;
    }

    return result;
}

constexpr std::array<std::uint8_t, 1> one_elem = {1};
constexpr std::array<std::uint8_t, 2> two_elem = {2, 3};
constexpr std::array<std::uint8_t, 3> all = concat(one_elem, two_elem);
Run Code Online (Sandbox Code Playgroud)

它在 C++14 中不起作用,因为std::array直到 C++17 才对 constexpr 友好。但是,如果您不关心最终结果是constexpr,则可以简单地将每个变量标记为const,这将起作用:

const std::array<std::uint8_t, 1> one_elem = {1};
const std::array<std::uint8_t, 2> two_elem = {2, 3};
const std::array<std::uint8_t, 3> all = concat(one_elem, two_elem);
Run Code Online (Sandbox Code Playgroud)

编译器几乎肯定会优化concat掉。

如果您需要 C++14 解决方案,我们必须通过std::array的构造函数创建它,所以它几乎没有那么好:

#include <array>
#include <cstdint>
#include <cstddef>
#include <type_traits>

// We need to have two parameter packs in order to
// unpack both arrays. The easiest way I could think of for
// doing so is by using a parameter pack on a template class
template <std::size_t... I1s>
struct ConcatHelper
{
    template <typename T, std::size_t... I2s>
    static constexpr std::array<T, sizeof...(I1s) + sizeof...(I2s)>
    concat(std::array<T, sizeof...(I1s)> const& lhs,
           std::array<T, sizeof...(I2s)> const& rhs,
           std::index_sequence<I2s...>)
    {
        return { lhs[I1s]... , rhs[I2s]... };
    }
};

// Makes it easier to get the correct ConcatHelper if we know a
// std::index_sequence. There is no implementation for this function,
// since we are only getting its type via decltype()
template <std::size_t... I1s>
ConcatHelper<I1s...> get_helper_type(std::index_sequence<I1s...>);

template <typename T, std::size_t N1, std::size_t N2>
constexpr std::array<T, N1 + N2> concat(std::array<T, N1> const& lhs, std::array<T, N2> const& rhs)
{
    return decltype(get_helper_type(std::make_index_sequence<N1>{}))::concat(lhs, rhs, std::make_index_sequence<N2>{});
}

constexpr std::array<std::uint8_t, 1> one_elem = {1};
constexpr std::array<std::uint8_t, 2> two_elem = {2, 3};
constexpr std::array<std::uint8_t, 3> all = concat(one_elem, two_elem);
Run Code Online (Sandbox Code Playgroud)

  • @vsoftco 并且您始终可以将其声明为 `constexpr auto all = ...` (3认同)
  • 你也可以不使用 `constexpr`,第一个解决方案在 C++14 中工作正常。您不在问题中使用 `constexpr`。 (2认同)

Bar*_*rry 5

已经有一种方法可以在 C++ 中连接数组:std::tuple_cat。唯一的问题是它给了你一个tuple<uint8_t, uint8_t, uint8_t>而不是一个std::array<uint8_t, 3>。但是这个问题可以用不同的标准库函数解决:std::apply. 从技术上讲,那个是 C++17,但可以在 C++14 中实现。你只需要一个 funject:

struct to_array_t {
    template <class T, class... Ts> 
    std::array<std::decay_t<T>, sizeof...(Ts)+1> operator()(T&& t, Ts&&... ts) const {
        return {{std::forward<T>(t), std::forward<Ts>(ts)...}};
    }   
} to_array{};
Run Code Online (Sandbox Code Playgroud)

然后你可以使用它:

auto all = std::apply(to_array, std::tuple_cat(one_elem, two_elem));
Run Code Online (Sandbox Code Playgroud)

隐藏在函数后面可能更容易:

template <class Target=void, class... TupleLike>
auto array_concat(TupleLike&&... tuples) {
    return std::apply([](auto&& first, auto&&... rest){
        using T = std::conditional_t<
            !std::is_void<Target>::value, Target, std::decay_t<decltype(first)>>;
        return std::array<T, sizeof...(rest)+1>{{
            decltype(first)(first), decltype(rest)(rest)...
        }};
    }, std::tuple_cat(std::forward<TupleLike>(tuples)...));
}
Run Code Online (Sandbox Code Playgroud)

使用 lambda 进行完美转发有点难看。的Target类型是允许用户指定所得到的阵列的类型-否则将被选择作为腐朽类型的第一个元素的。