在类模板特化中使用sizeof模板参数包
Using sizeof template parameters pack in class template specialization
在GCC上编译以下代码(根据从右到左1的索引从类型列表中选择类型的元函数)的正确性检查失败,而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:
部分专门化的非类型参数表达式不能包含部分专门化的模板参数,除非参数表达式是一个简单的标识符。
这已经被核心问题1315放宽了。大概GCC还没有开始实现它。
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 如何计算预处理器宏中的参数以传递给 sizeof?
- 在可变参数模板中使用 sizeof 运算符跳过递归结束的函数
- 返回取决于sizeof的变量类型..参数包
- 在visual c++中从无序映射继承时,模板参数的sizeof()不正确
- 为什么(void)sizeof(param)不"使用"参数?
- 对作为参数传递的数组使用sizeof
- 如何在带有模板参数的预处理器中使用sizeof
- 作为参数传递的数组sizeof的奇怪行为
- 在类模板特化中使用sizeof模板参数包
- <sizeof(T)> 作为 C++ 中继承的类模板参数