Is it possible to create a parameter package?

Consider the following pseudo code:

template<class... T> struct worker : unique<T...>::type...{}; struct x{}; struct y{}; struct z{}; 

Is it possible to write a unique template so that it generates a package of parameters consisting of only unique types among T s, so that worker<x,y,x,z> will be directly obtained from x , y , z respectively, in this order, if T are not final classes?

+4
source share
2 answers

AFAIK: No.

The problem is that type is the result of the typedef directive, and typedef cannot contain alias packages. This is actually worrying, and more often than not, computations on packages require the introduction of a shell type (for example, template <typename...> struct pack {}; ) in order to be able to pass them.

+6
source

Parameter packages cannot be easily saved , so I don’t think you can do what you want. However, since you seem to need this function to inherit from a set of bases, you can use metaprogramming of several templates to create a base type linearly inherited from all the bases in your set. Prior to this, you can easily filter duplicates from the options package.

Here is an implementation of this approach using Boost.MPL :

 #include <boost/mpl/fold.hpp> #include <boost/mpl/inherit.hpp> #include <boost/mpl/inherit_linearly.hpp> #include <boost/mpl/insert.hpp> #include <boost/mpl/placeholders.hpp> #include <boost/mpl/set.hpp> #include <boost/mpl/vector.hpp> namespace mpl = boost::mpl; template < typename ...Args > struct inherit_uniquely { // filter out duplicates typedef typename mpl::fold< mpl::vector< Args... >, mpl::set0<>, mpl::insert< mpl::_1, mpl::_2 > >::type unique_bases; // create base type typedef typename mpl::inherit_linearly< unique_bases, mpl::inherit< mpl::_1, mpl::_2 > >::type type; }; template < typename ...Bases > struct Derived : inherit_uniquely< Bases... >::type {}; struct X { int x;}; struct Y { int y;}; struct Z { int z;}; int main() { Derived< X, Y, Z > d; dx = 1; dy = 2; dz = 3; X & d_as_x = d; Y & d_as_y = d; Z & d_as_z = d; } 
+4
source

Source: https://habr.com/ru/post/1416096/


All Articles