在C++11草案的14.8.2.4p10中,有写
If for each type being considered a given template is at least as specialized for all types and more specialized for some set of types and the other template is not more specialized for any types or is not at least as specialized for any types, then the given template is more specialized than the other template.
为什么会有“或至少不专门用于任何类型”?据我所知,如果我们有一个类型列表
T1, T2, T3
U1, U2, U3
如果所有 T 至少都一样特化,而有些则更特化。并且没有一个 U 更特化,那么在我看来,从逻辑上讲,T 的集合作为一个整体比 U 的集合更特化。当我们没有一个至少比相应的 Ts 特化时,为什么还有提到的后备?
最佳答案
更新:现在已添加为 official C++ Issue
我终于弄清楚了如何阅读有问题的段落。下面我已经标出它了
If for each type being considered a given template is at least as specialized for all types, and
- more specialized for some set of types and the other template is not more specialized for any types, or
- {the other template} is not at least as specialized for any types,
then the given template is more specialized than the other template.
这样下面的第一个模板也比第二个模板更专业
template<typename T> void f(T*);
template<typename T> void f(T);
请注意,第一个模板的参数至少与第二个模板一样特化,但并未定义为“更特化” - 该术语仅适用于两个参数都是引用且适用某些条件的情况(参见第 9 段14.8.2.4) 。这些规则显然不要遵循任何正式的订购法。第二个模板至少不像第一个模板那样专门化。这意味着第二个项目符号适用,而不是第一个项目符号。
关于c++ - 什么时候一个模板比另一个模板更专业? 'And'/'Or' 与逻辑混淆。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15702143/