在C ++中创建类型的类型列表组合

时间:2019-03-05 14:26:55

标签: c++ c++14 variadic-templates template-meta-programming cartesian-product

我正在尝试创建一些工具来根据其他类型的组合创建类型列表。

让我们说我们有三种类型

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

我想获取一个元组列表,其中包含N,A,B或C类型的所有可能组合。

对于N = 2的情况,应该是

std::tuple<A,A>
std::tuple<A,B>
std::tuple<A,C>
std::tuple<B,A>
std::tuple<B,B>
std::tuple<B,C>
std::tuple<C,A>
std::tuple<C,B>
std::tuple<C,C>

这个想法是创建一个元组,为所有这些类型保存一个容器,以便以后可以在容器列表中存储任何这些类型。

template <typename ...Combinations>
using CombinationList = std::tuple<std::vector<Combinations>...>;

我已经有了一种机制,可以在适合的容器中插入特定的元素,但是我不知道如何创建组合。

人们在评论中建议使用std::vector<Combination<std::variant<A,C,B>, std::variant<A,B,C>>>。尽管这从技术上解决了问题,但我不愿使用它,因为A,BC和C的大小非常不同,我不想在运行时访问变体。另外,在某些时候,我需要将

中的所有数据上传到容器中
std::tuple<std::vector<Combination>...>

到GPU,所以我不能在这里使用std :: variant。

我该怎么办?

谢谢!

PD:这与此问题Combination explosion of an enum value (729 combinations...)有关 在这个问题中,我问我如何才能轻松生成将在容器内使用的类型。现在,我需要生成容器。

2 个答案:

答案 0 :(得分:5)

使用将二维矩阵存储在线性存储中的类比,ABC的所有可能对都由一维整数0,1,...,8标记,如下所示:

0 -> (0/3, 0%3) = (0,0) -> std::tuple<A,A>
1 -> (1/3, 1%3) = (0,1) -> std::tuple<A,B>
...
8 -> (8/3, 8%3) = (2,2) -> std::tuple<C,C>

因此,我们可以按以下方式构造对的列表。 这些函数可在C ++ 14及更高版本中使用。 例如,Combinations<A,B,C>::types等于std::tuple<std::vector<std::tuple<A,A>>, std::vector<std::tuple<A,B>>, ...>

Live DEMO

template<std::size_t I, typename Tuple>
struct make_pair_vector
{
    static constexpr std::size_t  left_index = I/std::tuple_size<Tuple>::value;
    static constexpr std::size_t right_index = I%std::tuple_size<Tuple>::value;

    using type = std::vector<
                    std::tuple<typename std::tuple_element< left_index, Tuple>::type,
                               typename std::tuple_element<right_index, Tuple>::type>>;
};

template <typename T, typename Is>
struct make_combinations;

template <typename Tuple, std::size_t... Is>
struct make_combinations<Tuple, std::index_sequence<Is...>>
{
    using tuples = std::tuple<typename make_pair_vector<Is, Tuple>::type...>;
};

template<typename ...Args>
struct Combinations
{
    using types = typename make_combinations
                    <std::tuple<Args...>,
                     std::make_index_sequence<(sizeof...(Args))*(sizeof...(Args))>>
                    ::tuples;
};

答案 1 :(得分:5)

  

我已经有了一种机制,可以在适合的容器中插入特定的元素,但是我不知道如何创建组合。

假设您有一个类型列表(例如A, B, C)和一个无符号整数N,我建议一个using

template <std::size_t N, typename ... Ts>
using Combinations = ???

定义为std::tuple,其中包含std::tuple的所有组合的列表。

例如,

Combinations<2u, A, B, C>

成为

  std::tuple<
      std::tuple<A,A>, std::tuple<A,B>, std::tuple<A,C>,
      std::tuple<B,A>, std::tuple<B,B>, std::tuple<B,C>,
      std::tuple<C,A>, std::tuple<C,B>, std::tuple<C,C>>

以下是完整的C ++ 11示例

#include <tuple>
#include <vector>
#include <type_traits>

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

template <typename T, typename ... Ts>
constexpr std::tuple<T, Ts...> addTupleType (std::tuple<Ts...>);

template <typename T, typename ... Ts>
constexpr auto addType ()
   -> std::tuple<decltype(addTupleType<T>(std::declval<Ts>()))...>;

template <typename ... Ts, typename ... Us>
constexpr auto getCombinations (std::integral_constant<std::size_t, 0u>,
                                std::tuple<Ts...> t, std::tuple<Us ...> u)
   -> decltype( u );

template <std::size_t N, typename ... Ts, typename ... Us,
          typename std::enable_if<(N > 0u), bool>::type = true>
constexpr auto getCombinations (std::integral_constant<std::size_t, N>,
                                std::tuple<Ts...> t, std::tuple<Us ...>)
   -> decltype (getCombinations(
         std::integral_constant<std::size_t, N-1u>{}, t,
         std::tuple_cat(addType<Ts, Us...>()...)));

template <std::size_t N, typename ... Ts>
using Combinations
   = decltype(getCombinations(
         std::integral_constant<std::size_t, N-1u>{},
         std::declval<std::tuple<Ts...>>(),
         std::declval<std::tuple<std::tuple<Ts>...>>()));

template <typename ... Ts>
constexpr auto CombListHelper (std::tuple<Ts...>)
   -> std::tuple<std::vector<Ts>...>;

template <typename T>
using CombinationList = decltype(CombListHelper(std::declval<T>()));


int main()
 {
   using type_1 = Combinations<2u, A, B, C>;
   using type_2 = std::tuple<
      std::tuple<A,A>, std::tuple<A,B>, std::tuple<A,C>,
      std::tuple<B,A>, std::tuple<B,B>, std::tuple<B,C>,
      std::tuple<C,A>, std::tuple<C,B>, std::tuple<C,C>>;

   static_assert( std::is_same<type_1, type_2>::value, "!" );

   using type_3 = CombinationList<Combinations<2u, A, B, C>>;
   using type_4 = std::tuple<
      std::vector<std::tuple<A,A>>, std::vector<std::tuple<A,B>>,
      std::vector<std::tuple<A,C>>, std::vector<std::tuple<B,A>>,
      std::vector<std::tuple<B,B>>, std::vector<std::tuple<B,C>>,
      std::vector<std::tuple<C,A>>, std::vector<std::tuple<C,B>>,
      std::vector<std::tuple<C,C>>>;

   static_assert( std::is_same<type_3, type_4>::value, "!" );
 }
相关问题