函数模板和模板函数有什么区别?

发布于 2024-07-26 23:50:03 字数 22 浏览 3 评论 0原文

函数模板和模板函数有什么区别?

What is the difference between function template and template function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏有森光若流苏 2024-08-02 23:50:03

术语“函数模板”是指模板的一种。 术语“模板函数”有时用于表示同一事物,有时用于表示从函数模板实例化的函数。 最好通过对前者使用“函数模板”和对后者使用“函数模板实例”或“函数模板实例”之类的内容来避免这种歧义。 请注意,函数模板不是函数。 同样的区别也适用于“类模板”与“模板类”。

来自此常见问题解答 (存档

The term "function template" refers to a kind of template. The term "template function" is sometimes used to mean the same thing, and sometimes to mean a function instantiated from a function template. This ambiguity is best avoided by using "function template" for the former and something like "function template instance" or "instance of a function template" for the latter. Note that a function template is not a function. The same distinction applies to "class template" versus "template class".

From this FAQ (archive)

一抹微笑 2024-08-02 23:50:03

函数模板是正确的术语(用于实例化函数的模板)。

模板函数是一个通俗的同义词。

所以,没有任何区别。

Function Template is the correct terminology (a template to instantiate functions from).

Template Function is a colloquial synonym.

So, there's no difference whatsoever.

故事和酒 2024-08-02 23:50:03

编译器根据函数模板(泛型函数)针对指定数据类型生成的函数称为模板函数。
例子:
下面的代码称为函数模板,因为它是函数的模板。

template<T>
T doubleVal(T a){
  return a+a;
 }
int main(){
  cout<<doubleVal<int>(5)<<endl;
}

当我们编译这段代码时,编译器将通过引用模板函数来编写一个 int 函数。 该函数称为模板函数。

The function generated by the compiler from function template(generic function) for specified data type is known as template function.
Example:
The below code is called function template as it is template for a function.

template<T>
T doubleVal(T a){
  return a+a;
 }
int main(){
  cout<<doubleVal<int>(5)<<endl;
}

When we compile this code compiler will write a function for int by taking reference from template function. That function is known as template function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文