2
template < int ...Indices>

class T1 {
    template <int _1, int _2>
    class T2;
};

template <int ...Indices>
template <int _1>
class T1<Indices...>::T2<_1, sizeof...(Indices)> {};
//^--error: non-type template argument depends on a template parameter of the partial specialization

在 gcc 4.5+ 上编译,但在 clang 3.1 和 icc 上都没有,都抱怨sizeof...(Indices). 它只是后者编译器中尚未实现的功能还是某些特殊情况?

谢谢,

布特

4

2 回答 2

1

该标准在 [temp.class.spec] 第 8 段中说

在类模板部分特化的参数列表中,适用以下限制:
— 部分特化的非类型参数表达式不应涉及部分特化的模板参数,除非参数表达式是简单标识符。[示例:

    模板 <int I, int J> 结构 A {};
    模板 <int I> 结构 A<I+5, I*2> {}; // 错误
模板 <int I, int J> 结构 B {}; 模板 <int I> 结构 B<I, I> {}; // 好的
结束示例]

该规则的目的是禁止基于示例中的非平凡表达式的部分特化,并且sizeof...(Indices)不是简单的标识符,因此可能 clang 和 ICC 拒绝它是正确的。老实说,我不确定哪个编译器是正确的。我建议向其中一个编译器报告错误,如果他们说他们的实现是正确的,请向其他编译器报告,以便以不同的方式解释它!

于 2012-05-06T15:43:58.300 回答
0

您可以尝试尝试:

template < int ...Indices>
class T1 
{
    static const int index_size = sizeof...(Indices);

    template <int _1, int _2>
    class T2;
};

template <int ...Indices>
template <int _1>
class T1<Indices...>::T2<_1, T1<Indices...>::index_size> {};

并查看编译器问题是否与实际sizeof...运算符有关,或者是否与模板声明中使用它的方式有关。

于 2012-01-16T22:00:41.420 回答