相关疑难解决方法(0)

为什么不允许非类型参数中的部分特化使用嵌套模板参数

我有这个代码

template<int N, bool C = true>
struct A;

template<int N>
struct A<N, !(N % 5)> {
  /* ... */
};

// should work
A<25> a;
Run Code Online (Sandbox Code Playgroud)

也就是说,对于N可被整除的数字,5编译器应使用部分特化.但编译器不会接受部分特化,因为标准要求它拒绝这样的代码,其中部分特化的非类型参数引用参数而不仅仅是一个参数(比如,A<N, N>是有效的).但是这样做的原因是什么?

请注意,我可以简单地将我的代码更改为更罗嗦的示例,它是有效的

template<bool> struct wrap;
template<int N, typename = wrap<true> >
struct A;

template<int N>
struct A<N, wrap<!(N % 5)> > {
  /* ... */
};

// should work
A<25> a;
Run Code Online (Sandbox Code Playgroud)

这很好,因为它不再是非类型参数.但是,规范禁止更直接的部分专业化的原因是什么?

c++ partial-specialization design-rationale

36
推荐指数
1
解决办法
3802
查看次数

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

我正在尝试使用 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 …

c++ templates sizeof template-specialization c++11

5
推荐指数
1
解决办法
1664
查看次数