嵌套模板特化取决于封闭模板参数

时间:2012-01-15 01:31:28

标签: c++ c++11 template-specialization nested-class variadic-templates

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)的使用。 在后面的编译器或某些特殊情况下,它只是一个尚未实现的功能吗?

谢谢,

Buote

2 个答案:

答案 0 :(得分:1)

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

  

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

    template <int I, int J> struct A {};
    template <int I> struct A<I+5, I*2> {};  // error
template <int I, int J> struct B {}; template <int I> struct B<I, I> {}; // OK
- 结束示例]

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

答案 1 :(得分: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...运算符有关,或者它是否与模板声明中使用的方式一致。