How to create a list of types to expand into a tuple?

I am trying to create a class with a tuple of all types that are passed to it. I want him to take a list of types as a template parameter and use the classes in this list, because the classes that the inner tuple will contain. I currently have something like this that doesn't actually compile.

template<class ... T> struct ComponentList {}; template<ComponentList<typename ...T> > class ComponentManager{ std::tuple<T...> components; }; 

The reason I want to have a ComponentList , to be its own type, is because I want to also go to other type lists later. Is it possible? If not, what alternative will work?

+7
c ++ c ++ 11 c ++ 14
source share
2 answers

You can add a template for reconfiguring parameters from your list of type types in std::tuple :

 template<class A, template<class...> class B> struct rebind_; template<template<class...> class A, class... T, template<class...> class B> struct rebind_<A<T...>, B> { using type = B<T...>; }; template<class A, template<class...> class B> using rebind = typename rebind_<A, B>::type; 

Then use it like this:

 template<class... T> struct ComponentList {}; template<class List> struct ComponentManager { rebind<List, std::tuple> components; }; int main() { using List = ComponentList<int, char, long>; ComponentManager<List> manager; std::cout << std::get<0>(manager.components) << '\n'; } 

I assume that if you want the source type to be a ComponentList , you can use enable_if and is_instantiation_of :

 template<class List, typename = std::enable_if<is_instantiation_of<List, ComponentList>::value>::type> struct ComponentManager { rebind<List, std::tuple> components; }; 
+6
source share

Could you ensure that all of your type list classes are of type tuple_t with their internal types? Something like:

 template <class ... T> struct ComponentList { typedef std::tuple<T...> tuple_t; }; template<class T> class ComponentManager { typename T::tuple_t components; }; 

Usage is what you expect:

 ComponentManager<ComponentList<int, double> > cm; 
+1
source share

All Articles