在 GCC 上,以下代码编译期间的正确性检查(通过从右到左从 1 开始的索引从类型列表中选择类型的元函数)在 GCC 上失败,而 clang 接受此代码:
#include <cstdlib>
template< std::size_t i, typename ...types >
struct at_index
{
};
template< typename first, typename ...rest >
struct at_index< (1 + sizeof...(rest)), first, rest... >
{
using type = first;
};
template< std::size_t i, typename first, typename ...rest >
struct at_index< i, first, rest... >
: at_index< i, rest... >
{
};
int main()
{
}
哪个编译器是正确的?
GCC 错误信息:
error: template argument '(1 + sizeof... (rest))' involves template parameter(s)
struct at_index< (1 + sizeof...(rest)), first, rest... >
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我认为 sizeof...
运算符中参数包中的类型不应解析为符号名称的某些部分。因此,此处不应存在名称混淆问题。
这是允许上述代码的clang扩展吗?
最佳答案
N4140 [temp.class.spec]/8.1:
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.
这已经被 core issue 1315 放宽了.大概 GCC 还没有开始实现它。
关于c++ - 在类模板特化中使用 sizeof 模板参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40616223/