【问题标题】:Using sizeof template parameters pack in class template specialization在类模板特化中使用 sizeof 模板参数包
【发布时间】:2016-11-15 17:35:37
【问题描述】:

在编译以下代码期间检查正确性(用于从从右到左从 1 开始的索引从类型列表中选择类型的元函数)在 GCC 上失败,而 clang em> 接受此代码:

#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 扩展吗?

【问题讨论】:

  • 无论参数顺序如何都会出现问题example
  • 解决方法可能是使用std::integral_constant 而不是非类型模板参数example。不确定您的代码是否有效...

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


【解决方案1】:

N4140 [temp.class.spec]/8.1:

部分特化的非类型参数表达式不应涉及 偏特化的模板参数,除非 参数表达式是一个简单的标识符。

这已经被core issue 1315放宽了。大概 GCC 还没有开始实施。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 2018-11-26
    • 2018-06-25
    • 2015-02-19
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多