首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可变模板模板参数

可变模板模板参数
EN

Stack Overflow用户
提问于 2012-01-07 07:47:10
回答 1查看 1K关注 0票数 4

下面的代码不能使用clang 3.0编译,这是因为我做错了吗?是因为它在c++11中不被允许还是因为它在clang中不被支持?

代码语言:javascript
复制
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;
}

编译器错误:

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-07 10:22:08

三个问题:

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

代码语言:javascript
复制
template<int> class... Tail

而不是

代码语言:javascript
复制
typename... Tail

并且您需要使用private C<Tail...>而不是private C<Tail>显式地扩展参数包。

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

代码语言:javascript
复制
// base case
template < template <int> class Head>
struct C<Head> : public Head<1> { };

(这是针对Clang 3.0的编译)

现在完整的代码片段:

代码语言:javascript
复制
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;
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8765977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档