部分类专门化只是编写完全专门化的另一种方式吗?

Is partial class specialization just a different way of writing a full specialization?

本文关键字:专门化 另一种 方式吗 分类      更新时间:2023-10-16

这两种专门化在本质上是做同样的事情吗?

//generic:
template<class T>
struct A{
  ...
 }
template<class T>
struct A<int>{
  ...
 }
template<>
struct A<int>{
  ...
 } 

也就是说,在我看来,任何部分专门化都可以重写为完全专门化。

不,不是。在图灵的焦油坑里,每一种足够强大的编程技术都是等价的。

template<typename A, typename B>
struct foo:std::false_type {};
template<typename T>
struct foo<T,T>:std::true_type {};
这里的

,我的部分特化将两个参数映射为一个。更高级的东西,比如:

template<typename T>
struct foo<T,std::vector<T>>:std::integral_constant<int, 7> {};

也可以。

您甚至可以使用单个参数template:

来完成此操作。
template<typename T>
struct is_func_signature : std::false_type {};
template<typename R, typename... Args>
struct is_func_signature< R(Args...) >: std::true_type {};

,其中我们对许多类型参数进行模式匹配,并提取它们。

部分专门化是对第一个接口有效的参数进行模式匹配的游戏。