gpt4 book ai didi

c++ - 特化主模板时,更特化意味着什么?

转载 作者:行者123 更新时间:2023-12-04 15:10:52 26 4
gpt4 key购买 nike

C++ 模板特化规则提到特化必须比主模板更特化。以下#1 代码片段导致编译错误,表明第二行不是更专业,但最后一个代码片段(#2)看起来非常接近#1。两个代码片段都将 int N 特化为 0,那么为什么第一个片段被提示为“不更特化”?

// #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

最佳答案

对于 more specialized :

Informally "A is more specialized than B" means "A accepts a subset of the types that B accepts".

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

关于c++ - 特化主模板时,更特化意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65229927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com