The following are my 2 cents...
If you want a generic solution, the bigger problem I see is that from int_vals
and char_vals
types isn't easy (in C++11; simpler in C++17) extract the types of the contained values (int
and char
).
So I suppose you have to pass they to magic<>
together with foo
and bar
(if you don't want foo
and bar
hard-coded).
So the call to magic<>
become (in my way)
using result_t
= typename magic<int, char, foo, bar, int_vals, char_vals>::type;
The following is a full working example with my solution.
#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, "");
}
Obviously, if you prefer hard-code some types (u_list
, c_list
, foo
, and bar
), the solution become a lot simpler
#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, "");
}