【问题标题】:Variadic template template arguments可变模板模板参数
【发布时间】:2012-01-06 23:47:10
【问题描述】:

下面的代码用clang 3.0编译不出来,是不是我做错了?是因为c++11不允许还是因为clang不支持?

template<int OFFSET>
struct A {
    enum O { offset = OFFSET };
};

template < template <int T> class Head, typename... Tail>
struct C : public Head<1>, private C<Tail> { };

int main()
{
    C< A, A > c1;

    return 0;
}

编译器错误:

test3.cxx:99:42: error: template argument for template template parameter must be a class template or type alias template
    struct C : public Head<1>, private C<Tail> { };
                                         ^
test3.cxx:103:15: error: use of class template A requires template arguments
        C< A, A > c1;
              ^
test3.cxx:94:12: note: template is declared here
    struct A {
           ^
2 errors generated.

【问题讨论】:

  • Tail 是一个模板参数包,但您在实例化C&lt;&gt; 时从不扩展包——您希望C&lt;&gt; 实例化的类型是什么?

标签: c++ c++11 variadic-templates


【解决方案1】:

三个问题:

Tail 是模板的可变参数列表,而不是类型。所以应该是

template<int> class... Tail

而不是

typename... Tail

并且您需要使用private C&lt;Tail...&gt; 而不是private C&lt;Tail&gt; 显式扩展参数包。

您需要实现基本情况,因为当Tail... 为空时:

// base case
template < template <int> class Head>
struct C<Head> : public Head<1> { };

(这是使用 Clang 3.0 编译的)

现在是整段代码:

template<int OFFSET>
struct A {
    enum O { offset = OFFSET };
};

template < template <int> class Head, template<int> class... Tail>
struct C : public Head<1>, private C<Tail...> { };
template < template <int> class Head>
struct C<Head> : public Head<1> { };

int main()
{
    C< A, A > c1;
    return 0;
}

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 2012-03-28
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 2020-04-29
    • 2014-09-08
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多