How to create the Cartesian product of a type list?

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

    Maybe something like this:

    template  struct typelist { };
    
    template  struct typelist_cat;
    
    template 
    struct typelist_cat, typelist>
    {
        typedef typelist type;
    };
    
    
    template  struct product;
    
    template 
    struct product, typelist>
    {
        // the cartesian product of {S} and {Ts...}
        // is a list of pairs -- here: a typelist of 2-element typelists
        typedef typelist...> S_cross_Ts;
    
        // the cartesian product of {Ss...} and {Ts...} (computed recursively)
        typedef typename product, typelist>::type
            Ss_cross_Ts;
    
        // concatenate both products
        typedef typename typelist_cat::type type;
    };
    
    // end the recursion
    template 
    struct product, typelist>
    {
        typedef typelist<> type;
    };
    

    Now you should be able to use product, typelist>::type.

提交回复
热议问题