【发布时间】: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