使用函数重载和函数模板之间的区别 [英] Difference between using function overloading and function templates

查看:265
本文介绍了使用函数重载和函数模板之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



功能重载和功能之间的主要区别

模板?


谢谢!

[见 http://www.gotw.ca/resources /clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Hi,
What is the major difference between function overloading and function
templates?

Thanks!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

WittyGuy写道:
WittyGuy wrote:
函数重载和
之间的主要区别是什么?函数模板?
What is the major difference between function overloading and
function templates?




重载函数集是预定义的和有限的(有界)。从模板生成的函数集可以(并且通常是)无界限。


重载决策的规则略有不同。例如,

来确定模板是否可以代表一个可行的函数,该函数应该是b $ b参与名称解析,模板参数需要是

推断出来。如果他们无法推断,模板无法实例化

并且不是一个可行的功能。


我确定还有更多,但我只能'现在想不出来。


你有没有试过书?像C ++模板一样作者:D。Vandevoorde和

N. Josuttis ......非常好。


V



Overloaded function set is predefined and limited (bounded). The set
of functions generated from a template can (and usually is) unbounded.

The rules for overload resolution are slightly different. For example,
to determine if a template can represent a viable function that should
participate in the name resolution, the template arguments need to be
deduced. If they cannot be deduced, the template cannot be instantiated
and is not a viable function.

I am certain there are more, but I just can''t think of any at the moment.

Have you tried any books? Like "C++ Templates" by D. Vandevoorde and
N. Josuttis... It''s very good.

V

那么,函数模板是否比函数重载好得多? (在

表现?)


Wg

So, is function template is much better than function overloading? (in
performance?)

Wg


WittyGuy写道:
WittyGuy wrote:

函数重载和函数模板之间的主要区别是什么?

谢谢!
Hi,
What is the major difference between function overloading and function
templates?

Thanks!




我会说主要函数模板和

重载函数之间的区别在于编译器在
$中寻找函数调用的匹配时会区分

函数模板b $ b源代码。如果可以,编译器将更喜欢调用

重载函数,而不必为源代码中出现的任何函数调用实例化一个函数

模板。


这种偏见的后果很有意思。特别令人惊讶的是

很多是专门的功能模板不考虑

匹配,除非它专门化的功能模板首先确定

成为所有候选功能的最佳匹配。这个策略的含义是,C ++编译器不会调用

专用模板函数 - 即使它的声明是完全的

匹配对于源代码中的函数调用 - 如果甚至有一个

函数重载,编译器认为比具有完全匹配的特殊化的

函数模板更好地匹配。好吧,

这是一口!


当然,人们可能会期望专门化

功能模板的原因每当源代码中的

函数调用使用完全匹配其专有类型的参数时,确保调用它。事实上,它是如何工作的 - 对于

重载函数(通常是一个重载函数是

开发人员真正想在这里写的。)


专门的函数模板不能确保函数匹配

调用。相反,专门的版本只告诉编译器如何为其特定的参数类型实例化函数模板,

它应该专门的函数模板是否是
a函数调用源代码。


格雷格

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[ comp.lang.c ++。主持。第一次海报:做到这一点! ]



I would say the "major" difference between a function template and an
overloaded function is that the compiler discriminates against a
function template whenever it seeks a match for a function call in the
source code. When it can, the compiler will prefer to call an
overloaded function instead of having to instantiate a function
template for any function call appearing in source code.

The consequences of this bias are interesting. Especially surprising to
many is that a specialized function template is not considered for a
match, unless the function template it specializes is first determined
to be the best match of all available candidate functions. The
implication of this policy is that the C++ compiler will not call a
specialized template function - even if its declaration is an exact
match for the function call in the source code - if there is even one
function overload that the compiler deems a better match than the
function template with the specialization that matches exactly. Well,
that was a mouthful!

Of course, one might expect that the reason for specializing the
function template would be to ensure that it gets called whenever a
function call in the source code uses parameters that match its
specialized types exactly. And in fact, that is how it works - for
overloaded functions (and usually an overloaded function is what the
developer really wanted to write here.)

A specialized function template is not to ensure a match for a function
call. Rather the specialized version merely tells the compiler how to
instantiate the function template for its particular parameter types,
should the function template it specializes ever be the best match for
a function call in the source code.

Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于使用函数重载和函数模板之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆