不言自明。
基本上,假设我有这样的类型列表:
using type_list_1 = type_list<int, somestructA>;
using type_list_2 = type_list<somestructB>;
using type_list_3 = type_list<double, short>;
它们可以是类型列表的可变数量。
如何获取笛卡尔积的类型列表?
result = type_list<
type_list<int, somestructB, double>,
type_list<int, somestructB, short>,
type_list<somestructA, somestructB, double>,
type_list<somestructA, somestructB, short>
>;
我确实涉足了如何创建双向笛卡尔积,如下所示: How to create the Cartesian product of a type list? ,但n方式似乎并不那么微不足道。
现在我正在努力...
template <typename...> struct type_list{};
// To concatenate
template <typename... Ts, typename... Us>
constexpr auto operator|(type_list<Ts...>, type_list<Us...>) {
return type_list{Ts{}..., Us{}...};
}
template <typename T, typename... Ts, typename... Us>
constexpr auto cross_product_two(type_list<T, Ts...>, type_list<Us...>) {
return (type_list<type_list<T,Us>...>{} | ... | type_list<type_list<Ts, Us>...>{});
}
template <typename T, typename U, typename... Ts>
constexpr auto cross_product_impl() {
if constexpr(sizeof...(Ts) >0) {
return cross_product_impl<decltype(cross_product_two(T{}, U{})), Ts...>();
} else {
return cross_product_two(T{}, U{});
}
}
我只想说,考虑到正确执行它是多么困难,只需使用巴里的答案中的升压即可。不幸的是,我必须坚持使用手动方法,因为要使用升压与否是来自其他地方的决定:(
最佳答案
与 Boost.Mp11 ,这是一句简短的话(一如既往):
using result = mp_product<
type_list,
type_list_1, type_list_2, type_list_3>;
Demo .
关于c++ - 如何在 C++ 中创建类型列表的 n 路笛卡尔积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59409722/