为什么C ++说不支持参数多态性? [英] Why is C++ said not to support parametric polymorphism?

查看:141
本文介绍了为什么C ++说不支持参数多态性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据参数多态性的维基百科页面:


类型多态性的一些实现在表面上类似于参数多态性,同时还引入特定方面。问题:为什么C ++表示只实现一个表面上类似于表达式的东西,而不是C ++模板的一个例子是C ++模板特化。

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



<参数化多态性?

Question: Why is C++ said to only implement something superficially similar to paramaterized polymorphism? In particular, aren't templates an example of full on parametric polymorphism?

推荐答案


为什么是C ++说只实现表面上类似于参数化多态性的东西?

Why is C++ said to only implement something superficially similar to parameterized polymorphism? In particular, aren't templates an example of full on parametric polymorphism?

C ++中的模板化函数在替换的基础上工作,的参数。这实质上意味着编译器生成另一个版本的函数,其中模板参数被硬编码到函数中。

Templated functions in C++ work on the basis of "substitution" of the parameter. Which essentially means that the compiler generates yet another version of the function where the template arguments are hardcoded into the function.

假设你在C ++中有这个:

Suppose you have this in C++:

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    int i = add(2, 3);
    double d = add(2.7, 3.8);
    return i + (int)d;
}



在编译期间,会产生两个函数: int add(int a,int b){return a + b; } double add(double a,double b){return a + b; }
一个函数只处理int,另一个函数只处理双精度。没有多态性。

During compilation, that will result in two functions: int add(int a, int b) { return a + b; } and double add(double a, double b) { return a + b; } One function will ONLY handle ints, and the other will ONLY handle doubles. No polymorphism.

真的,你最终得到的实现与参数变量的数量一样多。

So really, you end up with as many implementations as the number of argument variations.

你需要'add'函数的完整源代码,但是为什么不是这个参数多态性为了调用它与你自己的特定变化的东西重载二进制'+'运算符!

You need the full source code of the 'add' function, in order to call it with your own particular variation of something that overloads the binary '+' operator! - That's the detail that makes the difference.

如果C ++有正确的参数多态性,比如C#,你的最终编译实现'add '将包含足够的逻辑来确定在运行时'+'过载对于'add'可接受的任何给定参数。

If C++ had proper parametric polymorphism, like C# for instance, your final compiled implementation of 'add' would contain enough logic to determine at runtime what the '+' overload would be for any given parameter acceptable to 'add'. And you wouldn't need the source code for that function, to call it with new types you invented.

在现实中,这是什么意思?

但是不明白这是因为C ++不太强大或C#更强大。它只是许多语言特性细节之一。

But don't understand this as if C++ is less powerful or C# being more powerful. It's simply one of many language feature details.

如果你有完整的源代码可用于模板函数,那么C ++的语义是远远优越的。如果你只有一个静态或动态库供您使用,那么参数多态实现(例如C#)是优越的。

If you have the full source available for your templated functions, then C++'s semantics are far superior. If you only have a static or dynamic library at your disposal, then a parametric polymorphic implementation (e.g. C#) is superior.

这篇关于为什么C ++说不支持参数多态性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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