c++ - 在类模板特化中使用 sizeof 模板参数包

标签 c++ c++11 c++14 language-lawyer variadic-templates

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/

相关文章:

c++ - 这个 constexpr 虚函数技术是否违反了任何 C++11/C++14 规则?

c++ - 推导指南和带有可变模板构造函数的可变类模板 - 不匹配的参数包长度

c++ - 内联函数和一个定义规则

c++ - "Capture by move"不阻止引用捕获

C++ 统一赋值运算符移动语义

c++ - 在之前使用 emplace 而不是构造对象

c++ - 命名空间使用导致多重定义

c++ - 在 C++ 头文件中声明和定义静态变量?

c++ - BlackBerry 10开发-TextField

c++ - 位字段和值初始化导致缓冲区溢出 - 编译器错误或未定义的行为?