可变参数模板模板参数

4

以下代码在使用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.

1
“Tail”是一个模板参数包,但是在实例化“C<>”时你从未展开这个包 -- 你期望用什么类型来实例化“C<>”? - ildjarn
1个回答

5

三个问题:

Tail 应该是一个可变模板列表,而不是类型列表。因此应该是

template<int> class... Tail

替代

typename... Tail

您需要明确使用private C<Tail...>来扩展参数包,而不是private C<Tail>

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接