簡體   English   中英

在編譯時將兩個或多個不同大小的數組合並到一個數組中

[英]Combine two or more arrays of different size to one array at compiletime

我無法在現代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}

我會很高興看到任何有點容易閱讀的內容,例如

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

有辦法嗎? 我該怎么做才能解決這個問題?

如果您使用的是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);

它在C ++ 14中不起作用,因為在C ++ 17之前std::array不是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);

編譯器幾乎肯定會優化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);

已經有一種方法可以在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{};

然后你可以使用它:

auto all = std::apply(to_array, std::tuple_cat(one_elem, two_elem));

這可能更容易隱藏在一個函數后面:

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)...));
}

使用lambdas完美轉發有點難看。 Target類型允許用戶為結果數組指定類型 - 否則它將被選為第一個元素的衰減類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM