简体   繁体   English

为什么这不是功能模板的部分专业化?

[英]Why is this not a partial specialization of a function template?

Let's consider the following code: 让我们考虑以下代码:

template<typename T>
void func(T);
template<typename T>
void func(T*); // an overload

I know that the second declaration is an overload, not the partial specialization: 我知道第二个声明是重载,而不是部分专业化:

template<typename T>
void func<T*>(T*); // not allowed by C++ standard

But I wonder how is it different from a partial specialization? 但是我想知道它与部分专业化有何不同? It give us the same functionality as the partial specialization would give, doesn't it? 它为我们提供了与部分专业化所提供的功能相同的功能,不是吗?

But I wonder how is it different from a partial specialization? 但是我想知道它与部分专业化有何不同?

It's different in that it's not a specialization. 区别在于它不是专业。 A template specialization is an alternate implementation of a primary template, used when certain template arguments are provided. 模板专业化是主要模板的替代实现,在提供某些模板参数时使用。 When you apply a set of arguments to a template name in code, that causes the compiler to invoke template instantiation machinery. 当您将一组参数应用于代码中的模板名称时,这将导致编译器调用模板实例化机制。 Part of that is to check for explicit specializations of the primary template and use them if they match those arguments. 其中一部分是检查主模板的显式专业化,并在它们与那些参数匹配时使用它们。

With a function overload, that doesn't happen. 函数重载不会发生。 Overload resolution machinery is used instead. 而是使用了过载解析机。

Different means to accomplish the same ends. 实现相同目的的不同方法。

If you're wanting the details, a function is more than a name; 如果您需要详细信息,则功能不只是名称。 it is a name and its signature. 它是一个名称及其签名。 So a function with the same name but a different signature is a different function. 因此,具有相同名称但签名不同的功能就是不同的功能。 Thus, that line doesn't declare a specialization of the same template function; 因此,该行未声明相同模板函数的专业化; it declares a new primary template. 它声明了一个新的主模板。

It give us the same functionality as the partial specialization would give, doesn't it? 它为我们提供了与部分专业化所提供的功能相同的功能,不是吗?

There are probably cases where partial specialization could do something more conveniently than overloading, but those are probably based on SFINAE or other meta-programming tricks. 在某些情况下,部分专业化可以比重载更方便地做一些事情,但这些情况可能基于SFINAE或其他元编程技巧。 Yes, partial specialization could do most of what overloading does. 是的,部分专业化可以完成重载的大部分工作。

But you still need overloading rules, regardless of whether function partial specialization exists or not. 但是,无论是否存在函数部分专业化,您仍然需要重载规则。 You need those rules for non-template cases and cases where you can overload a template function with non-template versions. 对于非模板情况以及可以用非模板版本重载模板函数的情况,您需要这些规则。 Or that have more arguments or fewer than the primary template. 或者具有比主模板更多或更少的参数。 You need to be able to have some constructors which are templates and some which are not. 您需要能够拥有一些构造器,它们是模板,而有些则不是。

So there are going to be rules about how overloading works with template functions no matter what. 因此,无论如何,都有关于模板函数如何重载的规则。

So the better way to say what you're saying is that function overloading largely makes function partial specialization superfluous (which is probably why it doesn't exist). 因此,更好地表达您的意思是,函数重载很大程度上会使函数部分专业化成为多余的 (这可能就是为什么它不存在的原因)。 Or even better, class template partial specialization is a way to give class templates something similar to the overloading features of functions. 甚至更好的是,类模板的部分专业化是一种为类模板提供类似于函数的重载功能的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM