How to create the Cartesian product of a type list?

前端 未结 9 1440
夕颜
夕颜 2020-11-28 09:15

I\'d like to create the cross product of a list of types using variadic templates.

Here\'s what I have so far:

#include 
#include <         


        
9条回答
  •  眼角桃花
    2020-11-28 10:05

    Here's another solution.

    #include 
    #include 
    #include 
    
    template  struct typelist { };
    template  struct typepair { };
    
    template  struct product;
    template  struct append;
    
    template
    struct append, typelist> {
      typedef typelist type;
    };
    
    template<>
    struct product, typelist<>> {
      typedef typelist<> type;
    };
    
    template
    struct product, typelist> {
      typedef typelist<> type;
    };
    
    template
    struct product, typelist<>> {
      typedef typelist<> type;
    };
    
    template
    struct product, typelist> {
      typedef typename
              append,
                              typepair...,
                              typepair...>,
            typename product, typelist>::type>::type type;
    };
    
    int main(void)
    {
      int s;
      std::cout << abi::__cxa_demangle(
      typeid(product, typelist>::type).name(), 0, 0, &s)     << "\n";
      return 0;
    }
    

提交回复
热议问题