Is it possible to typedef a parameter package?

Is it possible to typedef a parameter package? for example

template<class T, class... Args> struct A { typedef T Type; // We typedef it, then its derived class can use it. // How about for parameter packs? // Option 1: typedef Args Arguments; // Option 2: using Arguments = Args; // Option 3: I can put in a tuple, but how can I untuple it to a pack typedef tuple<Args...> Tuple; }; 

I want to use the above method to implement the following

 template<int... VALUES> struct IntegralSequence { enum { SIZE = sizeof...(VALUES) }; template <unsigned I> struct At { enum { VALUE = typename tuple_element<I, tuple<integral_constant<int, VALUES>...>>::type::value }; }; }; template<unsigned N> struct AscendingSequence { typedef IntegralSequence<AscendingSequence<N-1>::VALUES..., N> Type; using VALUES = Type::VALUES; // if it works }; template<> struct AscendingSequence<1> { typedef IntegralSequence<0> Type; using VALUES = Type::VALUES; // if it works }; 
+7
c ++ c ++ 11 templates variadic-templates
source share
1 answer

You can pack them in tuple or in an arbitrary empty template template (I prefer to call it pack ):

 template<typename... Args> struct pack { }; template<class T, class... Args> struct A { using args = pack<Args...>; }; 

If you are then given A , for example. in the function template and you want to output Args... , you can do it like this:

 template<typename... Args, typename A> void f(pack<Args...>, A a) { /* use Args... here */ } template<typename A> void f(A a) { f(typename A::args(), a); } 

pack is empty, convenient in such situations. Otherwise, you will need another means to pass args without actually passing a tuple containing the data (for example, transferring it to another empty structure).

Or in class template specialization:

 template<typename T, typename = typename T::args> struct B_impl; template<typename T, typename... Args> struct B_impl <T, pack<Args...> > { // use Args... here }; template<typename T> using B = B_impl<T>; 

I think these are the deduction and partial specialization options mentioned by @dyp.


EDIT This is an answer to an edited question. Well, this is clearly an XY issue. If IntegralSequence is all you need, you can use std::make_integer_sequence in C ++ 14 or check my answer to another question just a few minutes ago for an efficient implementation.

+11
source share

All Articles