Some metaprogramming helpers for working with type lists:
template<class T>struct tag{using type=T;}; template<class Tag>using type=typename Tag::type; template<class...>struct types{using type=types;}; template<class...Ts> struct cat; template<class...Ts> using cat_t=type<cat<Ts...>>; template<class...As, class...Bs, class...Ts> struct cat< types<As...>, types<Bs...>, Ts... >: cat< types<As...,Bs...>, Ts... > {}; template<class...Ts> struct cat< types<Ts...> >: types<Ts...> {}; template<> struct cat<>: types<> {};
A way to display between sequences of values โโand sequences of types. I find types easier to work with:
template<class Seq> struct seq_to_types; template<class Seq> using seq_to_types_t=type<seq_to_types<Seq>>; template<class T, T...ts> struct seq_to_types< std::integer_sequence<T,ts...> >: tag< types< std::integral_constant<T,ts>... > > {}; template<class T, class Rhs> struct types_to_seq:tag<Rhs>{}; template<class T, class types> using types_to_seq_t=type<types_to_seq<T,types>>; template<class T, T...ts> struct types_to_seq<T, types<std::integral_constant<T, ts>...>>: tag<std::integer_sequence<T, ts...>> {}; template<class T, class...Ts> struct types_to_seq<T, types<Ts...>>: types< types_to_seq_t<T, Ts>... > {};
now we can take a std::integer_sequence<int, 1,2,3> and produce types< std::integral_constant<int,1>, std::integral_constant<int,2>, std::integral_constant<int,3> > which, in my opinion, is much easier to work with. We can even display a map.
This takes types<Ts...> and a function for types, and does this mapping:
template<template<class...>class M, class Seq> struct mapper; template<template<class...>class M, class Seq> using mapper_t=type<mapper<M,Seq>>; template<template<class...>class M, class...Ts> struct mapper<M, types<Ts...>>: types<M<Ts>...> {};
mapper_t< some_metafunction, types<blah...>> display each blah through some_metafunction to create a new list of types.
Next, a way to take a type function and associate the first argument with X :
template<template<class...>class F, class X> struct bind_1st { template<class...Ts> using apply=F<X,Ts...>; };
which can easily use cross-product (along with cat_t and mapper_t ):
template<class...Ts> struct cross_product:types<types<>>{}; template<class...Ts> using cross_product_t=type<cross_product<Ts...>>; template<class...T0s, class...Ts> struct cross_product<types<T0s...>, Ts...>:cat< mapper_t< bind_1st<cat_t, types<T0s>>::template apply, cross_product_t<Ts...> >... >{};
Now we are working on the next issue. We have a set of points, and we want to generate their cross product.
template<class...Seq> struct coords; template<class...Seq> using coords_t=type<coords<Seq...>>; template<class T, T...ts, class...Ts> struct coords< std::integer_sequence<T,ts...>, Ts... >: types_to_seq< T, cross_product_t< seq_to_types_t<std::integer_sequence<T,ts...>>, seq_to_types_t<Ts>... > > {};
should explode well.
living example .
The next step is to create the syntax.
template<class T, T t0, class Seq> struct offset_sequence; template<class T, T t0, class Seq> using offset_sequence_t=type<offset_sequence<T, t0, Seq>>; template<class T, T t0, T...ts> struct offset_sequence<T, t0, std::integer_sequence<T, ts...>>: tag<std::integer_sequence<T, (t0+ts)...>> {}; template<int start, int finish> using axis_limits = offset_sequence_t<int, start, std::make_integer_sequence<finish-start> >; template<class T> using point = std::vector<T>; template<class T, T...Is> point<T> make_point( std::integer_sequence<T, Is...> ) { return {Is...}; } template<class...Pts> std::vector<point<int>> make_space( types<Pts...> ) { return { make_point( Pts{} )... }; } template<class...Ts> std::vector<point<int>> generate_point_space() { return make_space( coords_t<Ts...>{} ); }
and we have the syntax you want.
We can do things in arrays and all constexpr if we want. Just change make_point to return a sizeof...(Is) array, etc.