unpacking variadic template arguments to define new template arguments

  • A+
Category:Languages

I am new to template programming and I have two questions...Hopefully someone can help me. I am trying to use variadic templates to generate a new input to another variadic template. In other words, I have a class

template <std::size_t N, std::size_t... M> class Class1 {  } 

I want to use the integer values represented by N,M to generate a new set of std::bitset type inputs to another templated class

template <typename T, typename... Ts> class Class2 {  } 

So for example if I use Class1<10,20,25> I want inside the body of Class1 to create a Class2<std::bitset<10>, std::bitset<20>, std::bitset<25>> variable. Is there a simple way to do this using C++11?

My second question then is how can I abstract this even more so that the unpacking is not specific to the std::bitset class? Is there a way to modify the Class1 template definition so that I can expand some arbitrary templated class that I develop instead of std::bitset?

 


You might write something like:

template <std::size_t N, std::size_t... M> class Class1 {     template <template <typename, typename...> class C,               template <std::size_t> class inner>     using rebind_with_type = C<inner<N>, inner<M>...>; }; 

And then

Class1<10, 20, 25>::rebind_with_type<Class2, std::bit_set> // -> Class2<std::bit_set<10>, std::bit_set<20>, std::bit_set<25>> 

And if call it with dependent name, don't forget typename/template:

typename Class1<N, M...>::template rebind_with_type<Class2, std::bit_set> // -> Class2<std::bit_set<N>, std::bit_set<M>...> 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: