Template templates templates with variable templates

I have a problem with template templates:

template <typename T> class A { }; template< template <typename> class T> class B { }; template <template <typename> class T, typename parm> class C { typedef T<parm> type; }; template <typename... types> class D { }; template <template <typename...> class T, typename ... parms> class E { typedef T<parms...> type; }; // How to pass list in list?? template < template <typename...> class ...T, ???> class F { }; 

Pass the type to the template first, no problem:

  A<int> a; //ok 

Now I want to create an instance from B, but not pass the template template template:

  B<A> b; // ok, but no chance to submit <int> inside A! 

Therefore, I need to expand the list of parameters:

  C<A, int> c; // ok, this transport int as parm into A 

Now I play with variation templates in a standard way:

  D<> d1; // ok D<int, float, double> d2; //ok 

The transfer of parameters to the variational part also goes forward:

  E<D> e1; //ok E<D, double, float, int> e2; //ok 

BUT: If I want to have a list of lists, I find no syntax that I can pass a list of parameters to a list of types. What I intend is something like this. but the above example also shows that B<A<int>> b; is a mistake! So the following example could not work :-(

  F< D< int, float>, D< int>, D <float, float, float> > f; 

My goal is to expand the list of lists using a specialized specialization. Any clues?

My solution after I understood the problem. Thanks!

Now I can deploy my Variadic variation template, as in the following example. A simple problem was that I was waiting for a template class, not for a simple type. Sometimes a solution can be so simple :-)

Here is my working result:

  template <typename ... > class D; template <typename Head, typename... types> class D<Head, types...> { public: static void Do() { cout << "AnyType" << endl; D<types...>::Do(); } }; template<> class D<> { public: static void Do() { cout << "End of D" << endl; } }; template < typename ...T> class H; template < typename Head, typename ...T> class H<Head, T...> { public: static void Do() { cout << "unroll H" << endl; cout << "Subtype " << endl; Head::Do(); H<T...>::Do(); } }; template <> class H<> { public: static void Do() { cout << "End of H" << endl; } }; int main() { H< D<int,int,int>, D<float, double, int> >::Do(); return 0; } 
+7
source share
2 answers

You can unpack one variation specialization at a time by specialization:

 template<typename...> struct S; template<> struct S<> { constexpr static int n = 0; }; template<template<typename...> class T, typename... Us, typename... Vs> struct S<T<Us...>, Vs...> { constexpr static int n = sizeof...(Us) + S<Vs...>::n; }; template<typename...> struct D {}; #include <iostream> int main() { std::cout << S<D<int, int, int>, D<int, int>, D<int>>::n << '\n'; // prints 6 } 
+2
source

Like this:

 template < template <typename...> class ...T > class F { }; int main() { F< D, D > f; } 

So, what F expects is a variational template class that takes another variational template class as an argument.

You cannot extend the argument arguments of an argument to a template class. If you specialize in a template class that you pass as an argument, you will get a custom version.

It:

  F< D< int, float>, D< int>, D <float, float, float> > f; 

does not work, since F expects a Variadic template class, taking Variadic template classes as types, and D< int, float> no longer a template (this is a specific class).

+1
source

All Articles