Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to "store" a template parameter pack without expanding it?

I was experimenting with C++0x variadic templates when I stumbled upon this issue:

template < typename ...Args > struct identities {     typedef Args type; //compile error: "parameter packs not expanded with '...' };  //The following code just shows an example of potential use, but has no relation //with what I am actually trying to achieve. template < typename T > struct convert_in_tuple {     typedef std::tuple< typename T::type... > type; };  typedef convert_in_tuple< identities< int, float > >::type int_float_tuple; 

GCC 4.5.0 gives me an error when I try to typedef the template parameters pack.

Basically, I would like to "store" the parameters pack in a typedef, without unpacking it. Is it possible? If not, is there some reason why this is not allowed?

like image 755
Luc Touraille Avatar asked Jan 14 '11 13:01

Luc Touraille


People also ask

How long template parameters will be valid?

3. What is the validity of template parameters? Explanation: Template parameters are valid inside a block only i.e. they have block scope.

Can template have default parameters?

You cannot give default arguments to the same template parameters in different declarations in the same scope. The compiler will not allow the following example: template<class T = char> class X; template<class T = char> class X { };

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

How do you expand a parameter pack?

Parameter packs can only be expanded in a strictly-defined list of contexts, and operator , is not one of them. In other words, it's not possible to use pack expansion to generate an expression consisting of a series of subexpressions delimited by operator , .


1 Answers

Another approach, which is slightly more generic than Ben's, is as follows:

#include <tuple>  template <typename... Args> struct variadic_typedef {     // this single type represents a collection of types,     // as the template arguments it took to define it };  template <typename... Args> struct convert_in_tuple {     // base case, nothing special,     // just use the arguments directly     // however they need to be used     typedef std::tuple<Args...> type; };  template <typename... Args> struct convert_in_tuple<variadic_typedef<Args...>> {     // expand the variadic_typedef back into     // its arguments, via specialization     // (doesn't rely on functionality to be provided     // by the variadic_typedef struct itself, generic)     typedef typename convert_in_tuple<Args...>::type type; };  typedef variadic_typedef<int, float> myTypes; typedef convert_in_tuple<myTypes>::type int_float_tuple;  int main() {} 
like image 156
GManNickG Avatar answered Sep 24 '22 01:09

GManNickG