21

Is it possible to typedef a parameter pack? 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 using the above technique 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
};
3
  • 3
    "I can put in a tuple, but how can I untuple it to a pack" With partial specialization, deduction or the indices trick.
    – dyp
    Apr 9, 2014 at 16:12
  • You can use Args... args;
    – user1551592
    Apr 9, 2014 at 16:19
  • If you can coarsely describe the use case, i.e. where you want to unpack them, it might be easier to construct a working example. (@dyp gave three possibilities but they are each suited for a limited set of use cases)
    – leemes
    Apr 9, 2014 at 16:19

1 Answer 1

26

You can pack them in a tuple, or in a arbitrary empty class 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 e.g. in function template and you want to deduce 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 being empty is convenient in situations like that. Otherwise you'd need some other means to pass args without actually passing a tuple that contains data (e.g. wrapping it into yet another empty struct).

Or, in a 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 guess these are the options of deduction and partial specialization that @dyp mentioned.


EDIT This is in response to the edited question. Ok, this is clearly an XY problem. 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.

6
  • 2
    +1 for using wrapper type other than tuple, it's all about separation of concerns.
    – Casey
    Apr 9, 2014 at 16:26
  • Just want to implement make_integer_sequence by myself and pops out this question. Apr 9, 2014 at 16:44
  • @user1899020 I don't believe it! This looks like an X-ABCDEF-Y problem :-)
    – iavr
    Apr 9, 2014 at 16:47
  • As a bikeshed, I like calling pack types: describe its contents, not its source, sort of. Plus types are not the only kind of parameter pack. Apr 9, 2014 at 16:55
  • 1
    @Yakk True. Unfortunately, in my projects, I have a namespace called types where all metaprogramming takes place :-)
    – iavr
    Apr 9, 2014 at 17:36

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.