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?
3. What is the validity of template parameters? Explanation: Template parameters are valid inside a block only i.e. they have block scope.
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 { };
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.)
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 , .
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() {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With