如何在 C++ 中创建类型列表的笛卡尔积?

the*_*ang 26 c++ templates variadic-templates c++17

不言自明。

基本上,假设我有这样的类型列表:

using type_list_1 = type_list<int, somestructA>;
using type_list_2 = type_list<somestructB>;
using type_list_3 = type_list<double, short>;
Run Code Online (Sandbox Code Playgroud)

它们可以是可变数量的类型列表。

如何获得笛卡尔积的类型列表?

result = type_list<
type_list<int, somestructB, double>,
type_list<int, somestructB, short>,
type_list<somestructA, somestructB, double>,
type_list<somestructA, somestructB, short>
>;
Run Code Online (Sandbox Code Playgroud)

我确实涉足了如何创建这里给出的双向笛卡尔积:如何创建类型列表的笛卡尔积?,但 n 方式似乎没有那么微不足道。

目前我正在尝试...

using type_list_1 = type_list<int, somestructA>;
using type_list_2 = type_list<somestructB>;
using type_list_3 = type_list<double, short>;
Run Code Online (Sandbox Code Playgroud)

我只想说,考虑到正确处理的难度,只需使用巴里的答案中的 boost 即可。不幸的是,我不得不坚持使用手动方法,因为是否使用 boost 是来自其他地方的决定:(

Bar*_*rry 14

使用Boost.Mp11,这是一个简短的单行(一如既往):

using result = mp_product<
    type_list,
    type_list_1, type_list_2, type_list_3>;
Run Code Online (Sandbox Code Playgroud)

演示

  • @Barry:从软件工程的角度来看,100% 同意。与手动方法相比,这种方法更容易阅读。此外,几乎不需要任何测试来确保库解决方案的正确性。总体而言,更少的代码和更高的置信度将导致其生命周期内的维护成本更低。 (9认同)
  • 天啊……但我觉得有必要指出(在 godbolt 上对每个代码进行多次采样)Mp11 版本的编译时间大约是原来的两倍。不确定有多少开销是解析 boost 标头本身以及有多少开销是实例化模板...... (2认同)

Max*_*hof 13

好的,我知道了。它不漂亮,但它有效:

template<class ... T>
struct type_list{};

struct somestructA{};
struct somestructB{};

using type_list_1 = type_list<int, somestructA, char>;
using type_list_2 = type_list<somestructB>;
using type_list_3 = type_list<double, short, float>;

template<class TL1, class TL2>
struct add;

template<class ... T1s, class ... T2s>
struct add<type_list<T1s...>, type_list<T2s...>>
{
    using type = type_list<T1s..., T2s...>;
};

template<class ... TL>
struct concat;

template<class TL, class ... TLs>
struct concat<TL, TLs...>
{
    using type = typename add<TL, typename concat<TLs...>::type>::type;
};

template<class TL>
struct concat<TL>
{
    using type = TL;
};

static_assert(std::is_same_v<type_list<int, somestructA, char, double, short, float>, typename add<type_list_1, type_list_3>::type>);

template<class TL1, class TL2>
struct multiply_one;

// Prepends each element of T1 to the list T2.
template<class ... T1s, class ... T2s>
struct multiply_one<type_list<T1s...>, type_list<T2s...>>
{
    using type = typename concat<type_list<type_list<T1s, T2s...>...>>::type;
};

static_assert(std::is_same_v<
    type_list<
        type_list<int, double, short, float>,
        type_list<somestructA, double, short, float>,
        type_list<char, double, short, float>
        >,
    typename multiply_one<type_list_1, type_list_3>::type>);

// Prepends each element of TL to all type lists in TLL.
template<class TL, class TLL>
struct multiply_all;

template<class TL, class ... TLs>
struct multiply_all<TL, type_list<TLs...>>
{
    using type = typename concat<typename multiply_one<TL, TLs>::type...>::type;
};

static_assert(std::is_same_v<
    type_list<
        type_list<int, double, short, float>,
        type_list<somestructA, double, short, float>,
        type_list<char, double, short, float>
        >,
    typename multiply_all<type_list_1, type_list<type_list_3>>::type>);

static_assert(std::is_same_v<
    type_list<
        type_list<int, somestructB>,
        type_list<somestructA, somestructB>,
        type_list<char, somestructB>,
        type_list<int, double, short, float>,
        type_list<somestructA, double, short, float>,
        type_list<char, double, short, float>
        >,
    typename multiply_all<type_list_1, type_list<type_list_2, type_list_3>>::type>);

template<class TL, class ... TLs>
struct cartesian_product
{
    using type = typename multiply_all<TL, typename cartesian_product<TLs...>::type>::type;
};

template<class ... Ts>
struct cartesian_product<type_list<Ts...>>
{
    using type = type_list<type_list<Ts>...>;
};


using expected_result = type_list<
    type_list<int, somestructB, double>,
    type_list<somestructA, somestructB, double>,
    type_list<char, somestructB, double>,
    type_list<int, somestructB, short>,
    type_list<somestructA, somestructB, short>,
    type_list<char, somestructB, short>,
    type_list<int, somestructB, float>,
    type_list<somestructA, somestructB, float>,
    type_list<char, somestructB, float>
>;

static_assert(std::is_same_v<expected_result,
    cartesian_product<type_list_1, type_list_2, type_list_3>::type>);
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/z/L5eamT

我把自己的static_assert测试留在那里... 好吧,我希望它们能有所帮助。

另外,我确定必须有更好的解决方案。但这是显而易见的“我知道这最终会导致目标”的路径。我最终不得不求助于添加 aconcat或 sorts,我确信可以更早地使用它来跳过大部分 cruft。

  • 我可以遵循的模板编程。棒极了。今天我学到了一些东西。 (4认同)

Pas*_* By 9

再次折叠表达式来救援

template<typename... Ts>
typelist<typelist<Ts>...> layered(typelist<Ts...>);

template<typename... Ts, typename... Us>
auto operator+(typelist<Ts...>, typelist<Us...>)
    -> typelist<Ts..., Us...>;

template<typename T, typename... Us>
auto operator*(typelist<T>, typelist<Us...>)
    -> typelist<decltype(T{} + Us{})...>;

template<typename... Ts, typename TL>
auto operator^(typelist<Ts...>, TL tl)
    -> decltype(((typelist<Ts>{} * tl) + ...));

template<typename... TLs>
using product_t = decltype((layered(TLs{}) ^ ...));
Run Code Online (Sandbox Code Playgroud)

你已经完成了。这比具有 O(1) 实例化深度的递归具有额外的好处。

struct A0;
struct A1;
struct B0;
struct B1;
struct C0;
struct C1;
struct C2;

using t1 = typelist<A0, A1>;
using t2 = typelist<B0, B1>;
using t3 = typelist<C0, C1, C2>; 

using p1 = product_t<t1, t2>;
using p2 = product_t<t1, t2, t3>;

using expect1 = typelist<typelist<A0, B0>,
                         typelist<A0, B1>,
                         typelist<A1, B0>,
                         typelist<A1, B1>>;

using expect2 = typelist<typelist<A0, B0, C0>,
                         typelist<A0, B0, C1>,
                         typelist<A0, B0, C2>,
                         typelist<A0, B1, C0>,
                         typelist<A0, B1, C1>,
                         typelist<A0, B1, C2>,
                         typelist<A1, B0, C0>,
                         typelist<A1, B0, C1>,
                         typelist<A1, B0, C2>,
                         typelist<A1, B1, C0>,
                         typelist<A1, B1, C1>,
                         typelist<A1, B1, C2>>;

static_assert(std::is_same_v<p1, expect1>);
static_assert(std::is_same_v<p2, expect2>);
Run Code Online (Sandbox Code Playgroud)