sizeof... 是否允许在模板参数中进行专门化?

Pub*_*bby 5 c++ templates sizeof template-specialization c++11

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

\n\n
template <int n, int... xs>\nstruct foo { \n  static const int value = 0;\n};\n\n// partial specialization where n is number of ints in xs:\n\ntemplate <int... xs>\nstruct foo<sizeof...(xs), xs...> { // error: template argument \xe2\x80\x98sizeof (xs ...)\xe2\x80\x99\n                                   //  involves template parameter(s)\n  static const int value = 1;\n};\n\ntemplate <int... xs>\nstruct foo<sizeof(xs), xs...> { // This compiles fine. sizeof(xs) is sizeof int \n                                // even though packs aren't expanded\n  static const int value = 2;\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n

Pub*_*bby 3

阅读这篇文章后,我将假设这是编译器问题。

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

是有争议的。

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

对我提交的错误报告的回复可能会有所帮助。