Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to transform the types in a parameter pack?

Is it possible to transform the types of a parameter pack and pass it on?

E.g. given the following:

template<class... Args> struct X {};
template<class T> struct make_pointer     { typedef T* type; };
template<class T> struct make_pointer<T*> { typedef T* type; };

Can we define a template magic or something similar so that the following assertion holds:

typedef magic<X, make_pointer, int, char>::type A;
typedef X<int*, char*> B;
static_assert(is_same<A, B>::value, ":(");
like image 901
Georg Fritzsche Avatar asked Jul 17 '10 22:07

Georg Fritzsche


People also ask

What is a parameter pack in C++?

Parameter packs (C++11) A parameter pack can be a type of parameter for templates. Unlike previous parameters, which can only bind to a single argument, a parameter pack can pack multiple parameters into a single parameter by placing an ellipsis to the left of the parameter name.

What is Variadic template in C++?

Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue.

What is Pack expansion?

Pack expansion A pattern followed by an ellipsis, in which the name of at least one parameter pack appears at least once, is expanded into zero or more comma-separated instantiations of the pattern, where the name of the parameter pack is replaced by each of the elements from the pack, in order. template<class...


1 Answers

Yes we can do that

template<template<typename...> class List, 
         template<typename> class Mod, 
         typename ...Args>
struct magic {
    typedef List<typename Mod<Args>::type...> type;
};
like image 144
Johannes Schaub - litb Avatar answered Oct 14 '22 08:10

Johannes Schaub - litb