gpt4 book ai didi

c++ - 嵌套模板特化取决于封闭的模板参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:33:34 24 4
gpt4 key购买 nike

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) 的使用。它只是后者编译器中尚未实现的功能还是某些特殊情况?

谢谢,

布特

最佳答案

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

Within the argument list of a class template partial specialization, the following restrictions apply:
— A partially specialized non-type argument expression shall not involve a template parameter of the partial specialization except when the argument expression is a simple identifier. [ Example:

    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
end example ]

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

关于c++ - 嵌套模板特化取决于封闭的模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8866881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com