簡體   English   中英

如何在C ++中找到可變參數類型列表的所有組合

[英]How to find all combinations of variadic typelist in C++

我想編寫一個C ++元函數,給出一個類型列表,沒有重復的元素,生成所有可能的子列表,其大小sizeof...(list) - 1

更新 :我正在尋找元函數find_all的通用實現,如下所述。 我現在的實現只是專門(硬編碼)最多5個元素的列表的結果,這顯然不是通用的。

下面是一些簡單的定義和一個例子:

給定一個存儲類型列表的容器:

template<typename... Ts>
struct type_list{};

它的一個“實例”:

struct A{};
struct B{};
struct C{};

using ABC = type_list<A, B, C>::type;

並使用名為find_all的元函數:

using found_result = find_all<ABC>;

結果應該是:

using expected_result = type_list<
  type_list<A, B>,
  type_list<A, C>,
  type_list<B, C>
>;

static_assert(
  std::is_same<
    expected_result,
    found_result
  >::value, "");

如果生成的子列表中的元素或子列表本身處於不同的順序,只要排序是確定性的,則是可以接受的。 因此以下結果也是有效的,例如:

using another_possible_expected_result = type_list<
  type_list<B, A>,
  type_list<B, C>
  type_list<C, A>,
>;

我正在使用C ++ 11(clang 6.0,Linux x86_64),但使用C ++ 14就可以了。

如果它有幫助,我也使用brigand元編程庫,它有列表/集處理的有用功能,但我接受使用其他庫的答案。

如果您願意使用C ++ 14和Boost,可以使用Boost Hana輕松實現我的其他答案中的算法:

#include <utility>
#include <boost/hana.hpp>

namespace hana = boost::hana;

template <typename Tup>
constexpr auto penultimate_sized_sublists(Tup types)
{
    constexpr auto size = hana::size(types);
    // hana::range is unusable with hana::transform,
    // hence the conversion to a hana::tuple
    constexpr auto indices = hana::to_tuple(hana::make_range(hana::size_c<0>, size));

    return hana::transform(indices, [&](auto index) {
        return hana::remove_at(types, index);
    });
}

關於Godbolt的演示

我不熟悉Brigand,所以我使用了Kvasir :: mpl 無論如何,算法應該可以轉移到那里的任何TMP庫:

給定類型列表[a0, a1, ..., aN] ,生成列表:

[a1, a2, a3, ..., aN]
[a0, a2, a3, ..., aN]
[a0, a1, a3, ..., aN]
...
[a0, a1, a2, ..., aN-1]

換句話說,在I個結果列表的元素是原始列表與I除去th元素。


在使用Kvasir :: mpl的代碼中:

namespace mpl = kvasir::mpl;

// Produce a list of indices into the original list
template <typename C = mpl::listify>
using indices = mpl::size<mpl::make_int_sequence<C>>;

// Given a list, produce a list of functions which, when evaluated on the original list,
// would erase the corresponding element.
template <typename C = mpl::listify>
using erase_each_index = indices< // given the indices,
    mpl::transform<               // transform each index by
        mpl::cfe<mpl::erase>,     // producing mpl::erase<Index>
        C
    >
>;

template <typename C = mpl::identity>
using listify = mpl::cfe<mpl::list, C>;

template <typename Fn, typename Args>
using eager_call = mpl::call<
    mpl::unpack<Fn>,
    Args
>;

template <typename C = mpl::listify>
using penultimate_sized_sublists =  mpl::fork< // each path takes the original list
    // 1. produce a list of functions which erase their index
    erase_each_index<>,
    // 2. produce the original list, wrapped in an extra list
    listify<listify<>>,
    // Both 1 and 2 are passed here. We perform a cartesian product (that's why
    // we wrapped 2 in an extra list) to put the arguments together with the functions
    mpl::product<
        // Evaluate each function against the entire list
        mpl::cfe<eager_call>
    >
>;

Godbolt演示

static_assert(
    std::is_same<
        mpl::call<
            mpl::unpack<penultimate_sized_sublists<>>,
            mpl::list<A, B, C>
        >,
        mpl::list<
            mpl::list<B, C>,
            mpl::list<A, C>,
            mpl::list<A, B>
        >
    >::value,
    ""
);

暫無
暫無

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

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