【问题标题】:Is sizeof... allowed in template arguments for specialization?sizeof... 是否允许在模板参数中用于专业化?
【发布时间】:2011-11-16 05:09:55
【问题描述】:

我正在尝试使用 GCC 4.7 快照做一些类似的事情:

template <int n, int... xs>
struct foo { 
  static const int value = 0;
};

// partial specialization where n is number of ints in xs:

template <int... xs>
struct foo<sizeof...(xs), xs...> { // error: template argument ‘sizeof (xs ...)’
                                   //  involves template parameter(s)
  static const int value = 1;
};

template <int... xs>
struct foo<sizeof(xs), xs...> { // This compiles fine. sizeof(xs) is sizeof int 
                                // even though packs aren't expanded
  static const int value = 2;
};

这个错误很奇怪,因为 sizeof 而不是 sizeof... 在这种情况下有效。两者似乎都可以在编译时轻松计算。

编译器是否正确,我不能在模板参数中使用sizeof... 进行特化?

【问题讨论】:

  • 您的意思是sizeof(xs) 有效吗?我希望收到有关未扩展包的投诉。
  • @R.MartinhoFernandes 奇怪的是它确实有效。
  • 同样的错误,更简单的代码:ideone.com/BBKbw。第二个可能是编译器中的错误,或者只是sizeof(xs) == sizeof(int),因此不涉及模板参数。 - 作为一个实用的解决方案:value = sizeof...(xs) == n;?

标签: c++ templates c++11 sizeof template-specialization


【解决方案1】:

在阅读 this post 后,我将假设这是编译器问题。

部分特化的非类型实参表达式不应包含部分特化的模板形参,除非实参表达式是简单标识符。

哪个有争议here

GCC 要么错误地解压参数包,要么过早地评估 sizeof

Response to bug report I filed may be helpful.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多