这是部分模板专用化吗?

Is this partial template specialization?

本文关键字:专用      更新时间:2023-10-16

我试图通过一些例子来理解模板元编程中的类型函数。
我创建了一个从类型中删除引用的示例。

template <class T>
struct remove_reference
{ using type = T; };
template <class T>
struct remove_reference<T&>
{ using type = T; };
int main(){
typename remove_reference<int&>::type a;
}

我的问题是,这是否是使用部分模板专用化实现的,如果我们称之为其他东西?
我觉得这是部分的,因为我们没有为特定类型定义它,但我也觉得这不是因为我们有同样多的模板参数。 命名对于理解类型函数可能并不重要,但是如果我解释它,我不想教别人错误的名字。

是的,这是部分专业化,因为您仅限于与模式T&匹配的东西。

您不需要拥有更少的模板参数,甚至可以拥有更多

例如
template <typename Callable>
struct function_something { ... }; // Any functor type
template <typename Ret, typename Args...>
struct function_something<Ret(Args...)> { ... }; // Specialises free functions
template <typename Class, typename Ret, typename Args...>
struct function_something<Ret(Class::*)(Args...)> { ... }; // Specialises member functions