我正在尝试使用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...
进行专门化?
看完这篇文章后,我假设这是编译器的问题。
部分专门化的非类型实参表达式不能包含部分专门化的模板形参,除非实参表达式是一个简单的标识符。
这是有争议的
GCC不正确地解包参数,或者过早地计算sizeof
。
对我提交的bug报告的回应可能会有所帮助。