How to create the Cartesian product of a type list?

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

    With Boost.Mp11, this is a short one-liner (as always):

    using input = type_list;
    using result = mp_product<
        type_pair,
        input, input>;
    

    Demo.


    We can generalize this to picking N things, with repetition, from that input. We can't use type_pair anymore to group our elements, so we'll just have a list of type_list of elements. To do that, we basically need to write:

    mp_product
    //                    ~~~~~~~ N times ~~~~~~~~
    

    Which is also the same as:

    mp_product_q, input, input, ..., input>
    //                                ~~~~~~~ N times ~~~~~~~~
    

    One way to do that is:

    template 
    using product = mp_apply<
        mp_product_q,
        mp_append<
            mp_list>, 
            mp_repeat_c, N>
            >>;
    

    Demo.

提交回复
热议问题