为什么说C++不支持参数多态?

2024-03-15

根据维基百科页面参数多态性 https://en.wikipedia.org/wiki/Parametric_polymorphism:

类型多态性的一些实现表面上与参数多态性相似,同时也引入了特殊方面。 C++ 模板专门化就是一个例子。

问题:为什么说 C++ 只实现了表面上类似于参数化多态性的东西?特别是,模板不是完整参数多态性的示例吗?


您链接到的文章解释了这一点。您引用的文本实际上给出了一个将 C++ 模板与纯参数多态性区分开来的示例:C++ 模板专业化。

它继续这个主题:

Following Christopher Strachey,[2] parametric polymorphism may be contrasted with ad hoc polymorphism https://en.wikipedia.org/wiki/Ad_hoc_polymorphism, in which a single polymorphic function can have a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. Thus, ad hoc polymorphism can generally only support a limited number of such distinct types, since a separate implementation has to be provided for each type.

因此,正如所描述的,C++ 模板接近(但不完全)参数多态性。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么说C++不支持参数多态? 的相关文章

随机推荐