问题
I'm reading through Eric Niebler's post on his tiny metaprogramming library. In trying to implement the pieces that he omits / lists as challenges, I am left with the following implementation of transform
:
template <typename F, typename... As>
using meta_apply = typename F::template apply<As...>;
template <typename... >
struct typelist_transform;
// unary
template <typename... T, typename F>
struct typelist_transform<typelist<T...>, F>
{
using type = typelist<meta_apply<F,T>...>;
};
// binary
template <typename... T, typename... U, typename F>
struct typelist_transform<typelist<T...>, typelist<U...>, F>
{
using type = typelist<meta_apply<F,T,U>...>;
};
This works, but seems very unsatisfactory to me - I need a partial specialization of typelist_transform
for every number of "arguments" into F
. Is there a better way to implement this metafunction?
回答1:
With a slightly modified interface (taking the metafunction first, rather than last, and adding a couple members to typelist
to get its size and the N-th member rather than using standalone metafunctions):
template <typename... Ts>
struct typelist {
template<size_t N>
using get = std::tuple_element_t<N, std::tuple<Ts...>>;
static constexpr size_t size = sizeof...(Ts);
};
template<class, class, class...>
struct typelist_transform_helper;
template<size_t... Ints, class F, class...Lists>
struct typelist_transform_helper<std::index_sequence<Ints...>, F, Lists...>{
template <class MF, size_t N, typename... Ls>
using apply_to_Nth = meta_apply<MF, typename Ls::template get<N>...>;
using type = typelist<apply_to_Nth<F, Ints, Lists...>...>;
};
template<class F, class L1, class... Lists>
struct typelist_transform : typelist_transform_helper<std::make_index_sequence<L1::size>, F, L1, Lists...> {
// static assert on size of all lists being equal if wanted
};
Demo.
One problem with the above is that tuple_element
usually requires N
recursive template instantiations, making the total number of template instantiations quadratic in the length of the list. Given our use case (we access every single index), we can do better:
template<class L>
struct typelist_indexer {
template<size_t N, class T> struct helper_base { using type = T; };
template<class S, class> struct helper;
template<size_t... Ns, class... Ts>
struct helper<std::index_sequence<Ns...>, typelist<Ts...>> : helper_base<Ns, Ts>... {};
template<size_t N, class T>
static helper_base<N, T> do_get(helper_base<N, T>);
using helper_type = helper<std::make_index_sequence<L::size>, L>;
template<size_t N>
using get = typename decltype(do_get<N>(helper_type()))::type;
};
and then
template<size_t... Ints, class F, class...Lists>
struct typelist_transform_helper<std::index_sequence<Ints...>, F, Lists...>{
template <class MF, size_t N, typename... Ls>
using apply_to_Nth = meta_apply<MF, typename typelist_indexer<Ls>::template get<N>...>;
using type = typelist<apply_to_Nth<F, Ints, Lists...>...>;
};
Now the number of template instantiations is linear in the size of the list, and in my tests this result in a significant compile time improvement. Demo.
来源:https://stackoverflow.com/questions/30492808/folding-over-arbitrarily-many-variadic-packs