【问题标题】:What does more specialized mean when specializing a primary template?专业化主要模板时,更专业化意味着什么?
【发布时间】:2020-12-10 07:09:36
【问题描述】:

C++ 模板特化规则提到的特化必须比主模板更特化。跟随#1 代码 sn-p 导致编译错误,表示第二行不是更专业,但最后一个 sn-p (#2) 工作,看起来非常接近 #1。两个代码 sn-ps 都将 int N 专门化为 0,那么为什么第一个 sn-p 被抱怨为“没有更专门化”?

// #1
template<int N, typename T1, typename... Ts> struct B;
template<typename... Ts> struct B<0, Ts...> { }; // Error: not more specialized
// #2
template<int N, typename T1, typename... Ts> struct B;
template<typename T, typename... Ts> struct B<0, T, Ts...> { }; // this works

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    对于more specialized

    非正式地,“A 比 B 更专业”的意思是“A 接受 B 接受的类型的子集”。

    专业化可以接受的任何类型都应该是主模板可以接受的类型的子集。但是对于B&lt;0&gt;,它只能被特化接受,而主模板不能,因为模板参数T1是必不可少的。

    【讨论】:

      猜你喜欢
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 2011-09-23
      • 2014-07-06
      • 2019-01-29
      相关资源
      最近更新 更多