以下是我的2美分...
如果你想有一個通用的解決方案,我看到的更大的問題是,從int_vals
和char_vals
類型是不容易的(在C + + 11;在C++ 17中更簡單)提取包含值的類型(int
和char
)。
,所以我想你要通過他們與foo
和bar
一起magic<>
(如果你不想foo
和bar
硬編碼的那樣)。
所以調用magic<>
成爲(我的方式)
using result_t
= typename magic<int, char, foo, bar, int_vals, char_vals>::type;
以下是我的解決方案一個完整的工作示例。
#include <type_traits>
template <int...> struct u_list {};
template <char...> struct c_list {};
template <int, char> struct foo {};
template <typename ...> struct bar {};
template <typename T1, typename T2, T1 t1, T2 ... T2s>
struct midProd
{ };
template <typename T1, typename T2, template <T1, T2> class, typename...>
struct magicHelper;
template <typename T1, typename T2,
template <T1, T2> class ResIn,
template <typename...> class ResOut,
typename ... R>
struct magicHelper<T1, T2, ResIn, ResOut<R...>>
{ using type = ResOut<R...>; };
template <typename T1, typename T2,
template <T1, T2> class ResIn,
template <typename...> class ResOut,
typename ... R, T1 ts1, T2 ... ts2, typename ... MpS>
struct magicHelper<T1, T2, ResIn, ResOut<R...>,
midProd<T1, T2, ts1, ts2...>, MpS...>
{ using type = typename magicHelper<T1, T2, ResIn,
ResOut<R..., ResIn<ts1, ts2>...>, MpS...>::type; };
template <typename T1, typename T2,
template <T1, T2> class,
template <typename...> class,
typename, typename>
struct magic;
template <typename T1, typename T2,
template <T1, T2> class ResIn,
template <typename...> class ResOut,
template <T1...> class C1, template <T2...> class C2,
T1 ... ts1, T2 ... ts2>
struct magic<T1, T2, ResIn, ResOut, C1<ts1...>, C2<ts2...>>
{ using type = typename magicHelper<T1, T2, ResIn, ResOut<>,
midProd<T1, T2, ts1, ts2...>...>::type ; };
int main()
{
using int_vals = u_list<1, 5, 7>;
using char_vals = c_list<-3, 3>;
using result_t
= typename magic<int, char, foo, bar, int_vals, char_vals>::type;
using ref_t = bar< foo<1, -3>, foo<1, 3>,
foo<5, -3>, foo<5, 3>,
foo<7, -3>, foo<7, 3> >;
static_assert(std::is_same<result_t, ref_t >::value, "");
}
很顯然,如果你喜歡硬編碼某些類型(u_list
,c_list
,foo
和bar
),該解決方案變得非常簡單
#include <type_traits>
template <int...> struct u_list {};
template <char...> struct c_list {};
template <int, char> struct foo {};
template <typename ...> struct bar {};
template <int, char...> struct midProd {};
template <typename...>
struct magicH;
template <typename ... R>
struct magicH<bar<R...>>
{ using type = bar<R...>; };
template <typename ... R, int i, char ... cs, typename ... MpS>
struct magicH<bar<R...>, midProd<i, cs...>, MpS...>
{ using type = typename magicH<bar<R..., foo<i, cs>...>, MpS...>::type; };
template <typename, typename>
struct magic;
template <int ... is, char ... cs>
struct magic<u_list<is...>, c_list<cs...>>
{ using type = typename magicH<bar<>, midProd<is, cs...>...>::type; };
int main()
{
using int_vals = u_list<1, 5, 7>;
using char_vals = c_list<-3, 3>;
using result_t = typename magic<int_vals, char_vals>::type;
using ref_t = bar< foo<1, -3>, foo<1, 3>,
foo<5, -3>, foo<5, 3>,
foo<7, -3>, foo<7, 3> >;
static_assert(std::is_same<result_t, ref_t >::value, "");
}
我一直在做一些討厭的東西與參數組合...也許這可能會有所幫助:[\ [點擊我\]](https://stackoverflow.com/questions/39687907/is-it-possible -to-invoke-a-all-possible-k-combinations-with-repetit) –