可变参数模板的模板模板语法问题

Kla*_*aus 7 c++ templates variadic-templates c++11

我有可变参数模板模板的问题:

            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
            {
            };
Run Code Online (Sandbox Code Playgroud)

首先,将一个类型传递给模板,没问题:

            A<int> a; //ok
Run Code Online (Sandbox Code Playgroud)

现在,我想从B创建一个实例,但无法传递模板模板参数:

            B<A> b; // ok, but no chance to submit <int> inside A!
Run Code Online (Sandbox Code Playgroud)

所以我必须扩展参数列表:

            C<A, int> c; // ok, this transport int as parm into A
Run Code Online (Sandbox Code Playgroud)

现在我以标准方式使用可变参数模板:

            D<> d1; // ok
            D<int, float, double> d2;   //ok
Run Code Online (Sandbox Code Playgroud)

将参数传递到可变参数部分也是向前的:

            E<D> e1;    //ok
            E<D, double, float, int> e2; //ok
Run Code Online (Sandbox Code Playgroud)

但是:如果我想要一个列表列表,我发现没有语法可以将参数列表传递给类型列表.我的意图是这样的.而且上面的例子表明这B<A<int>> b;是一个错误!所以下面的例子不能工作:-(

            F< D< int, float>, D< int>, D <float, float, float> > f;
Run Code Online (Sandbox Code Playgroud)

我的目标是通过模板专业化展开列表列表.任何提示?

我理解了问题后的解决方案.谢谢!

现在我可以展开我的可变参数模板模板,如下例所示.简单的问题是,我等待模板类而不是简单类型.有时,解决方案可以很容易:-)

那是我现在的工作成果:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

eca*_*mur 2

您可以通过专业化一次解压一个可变参数专业化:

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
}
Run Code Online (Sandbox Code Playgroud)