gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:25:08 25 4
gpt4 key购买 nike

根据 Parametric Polymorphism 的维基百科页面:

Some implementations of type polymorphism are superficially similar to parametric polymorphism while also introducing ad hoc aspects. One example is C++ template specialization.

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

最佳答案

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

它继续这个主题:

Following Christopher Strachey,[2] parametric polymorphism may be contrasted with 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++ 模板接近于——但不完全是——参数多态性。

关于c++ - 为什么说C++不支持参数多态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com