How to create the Cartesian product of a type list?

前端 未结 9 1478
夕颜
夕颜 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:07

    Somehow my brain is fried: I think I'm using more code than is needed but, at least, it has the desired results (although I didn't fix the memory leak):

    #include 
    #include 
    #include 
    
    template struct type_list {};
    
    template struct type_pair {};
    
    template
      struct row
    {
      typedef type_list...> type;
    };
    
    template  struct concat;
    template 
    struct concat, type_list>
    {
        typedef type_list type;
    };
    
    template 
    struct expand
    {
        typedef type_list type;
    };
    template <> struct expand<> { typedef type_list<> type; };
    template 
    struct expand, L...>
    {
        typedef typename concat::type, typename expand::type>::type type;
    };
    
    template
      struct cross_product
    {
        typedef typename expand::type...>>::type type;
    
    };
    
    int main()
    {
      int s;
      typedef cross_product::type result;
      std::cout << abi::__cxa_demangle(typeid(result).name(), 0, 0, &s) << std::endl;
    
      return 0;
    }
    

提交回复
热议问题