I have a mpl::vector & want to instantiate a template using the vector elements as template arguments. How is this done? Can a argument pack be used to consolidate extra mpl::vector elements?
For example:
struct A; struct B; struct C; struct D;
using args = mpl::vector<A, B, C, D>;
template<typename argA, typename argB, typename argC...>
struct derived_type;
using type_from_vector = derived_type<args>;
What is the best way to approach something like this?
Thanks.
You could either use boost::mpl::fold
or std::make_index_sequence
.
Both these code snippets assume using namespace boost::mpl;
.
Using boost::mpl::fold
:
template <typename TList, typename T> struct ExtendTList;
template<typename T, typename... Ts>
struct ExtendTList<derived_type<Ts...>, T>
{
using type = derived_type<Ts..., T>;
};
using type_from_vector = fold<args, derived_type<>, ExtendTList<_1, _2>>::type;
Using std::make_index_sequence
:
template <typename V, template<typename...> T, typename Seq>
struct MakeFromTypesAtIndices;
template <typename V, template<typename...> T, size_t ... Indices>
struct MakeFromTypesAtIndices<V, T, std::integer_sequence<size_t, Indices...>>
{
using type = T< at<V, Indices>... >;
};
using type_from_vector = MakeFromTypesAtIndices<args, derived_type, std::make_index_sequence<size<args>::value>>::type;