Why is it disallowed for partial specialization in a non-type argument to use nested template parameters

本小妞迷上赌 提交于 2019-11-27 20:01:24

I think a lot of it is historical. Non-type template parameters weren't originally allowed at all. When they were added, there were lots of limitations. As people tried different possibilities, and confirmed that they didn't cause problems, some of the limitations were removed.

Some of those original limitations remain for no particular reason beyond the fact that nobody bothered to work at changing them. Much like there, many of them can be worked around so removing them generally often wouldn't cause any particular difficulty. Mostly it comes down to a question of whether anybody cared enough about this particular case to write a paper about it.

Tom Wilson

Partial specialisation requires that the non-type template argument be resolvable at compile time.

At this point

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

N is a variable that can take more than one value and the compiler is unable to calculate N % 5 with certainty.

Your example instantiates a using

A<25> a;

but you could also have

A<25> a1;
A<15> a2;

How does the compiler choose a value for N in this scenario? It can't and so it has to disallow the code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!