Storing parameter packs - GerdHirsch/GenericTools GitHub Wiki

Parameterpacks <class ...pack>cannot easily stored via a typedef or a using declaration like any type. To enable storing, manipulating and passing parameterpacks the template Typepack without a definition is used just to specialize another template for that ( see also 2) ):

template<class ...> struct Typepack;

// primary template with a type parameter ...pack
template<class ...pack>
class Widget{ /* definition*/ };

// Specialization for Typepack<...>
template<class ...pack>
class Widget<Typepack<pack...>> 
: Widget<pack...>{}; // delegation by inheritance

A template must support the Typepack<...> parameter via a specialization. The standard implementation for this specialization is "delegation by inheritance". Because the specialization inherits from its primary template, it is assignment compatible but not vice versa. Without inheritance, the two types were completely independent from each other or "invariant".

 //usage:
class A; class B; class C;
// storing the pack
using pack = Typepack<A, B, C>;

Widget<A, B, C> w1;
// error: no matching function for call to
// 'Widget<Typepack<A, B, C> >::Widget(Widget<A, B, C>&)'
//	Widget<pack> w2a(w1);
Widget<pack> w2;
Widget<A, B, C> w1a(w2);

w1 = w2;
// error: no match for 'operator='
// (operand types are 'Widget<Typepack<A, B, C> >' and 'Widget<A, B, C>')
//	w2 = w1;

How to avoid this incompatibility is described in Preparing Types for usage with template Typepack. For type-functions like Reverse<..> the incompatibility doesn't matter.

next

⚠️ **GitHub.com Fallback** ⚠️