How can I define arguments of variadic template?

Viewed 72

I have a variadic template and would to define the arguments using using alias declaration.

Here is an example:

template<class I, class... P>
struct Molecule {
  using Index = I;
};

My question is, how can I define the first argument of P... ?

I have already tried this, but it generates an error:

template<class I, class... P>
struct Molecule {
  using Index = I;
  P array[sizeof...(P)] = { P... };
  using Part1 = array[0];
};

Any suggestions please?

1 Answers

how i can define the first argument of P... ?

If the Molecule can be redefined to the following, you get it directly:

template<class I, class First, class... P> { 
    using Part1 = First;
    // .....
}

Otherwise, make a trait to find the first type:

template<typename First, typename... Rest> 
struct first { using type = First; };
template<typename... Args> using first_t = typename first<Args...>::type;

Now

template<class I, class... P> struct Molecule {  
    using Part1 = first_t<P...>; // first type
};

assuming that you would have at least one argument in variadic P....

See a demo

Related