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.
4

1 回答 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;
}
于 2012-01-07T02:22:08.240 回答