静态多态定义和实现 [英] Static polymorphism definition and implementation

查看:140
本文介绍了静态多态定义和实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些关于静态多边形的概念的问题,我有时会听到;您可能主要在C ++的语境中解释它们,但是如果适用,我会非常感谢与语言无关的答案(因此标记C ++和语言不可知的)。


  1. 我们如何定义静态多态性?例如,我相信 std :: sort 函数应该被认为是静态多态的,因为它取决于某些对象的某些接口,这些对象的行为像迭代器,并且确切的行为在提供的迭代器的接口下,可以在编译时确定。这个解释我们如何定义静态多态性,还是只是一个具体案例的描述,还有更多的呢?


  2. 在C ++中使用静态多态的代码模式还有:仅通过C ++中的模板实现SP


  3. 确实,一个给定的UML类图不直接描述多态性的处理方式因此,它可以至少部分地被静态或动态地实现?换句话说:静态vs动态多态性的选择是否与OOP模型无关,因此决定了实现者?


  4. 是静态的多态性只有C ++特定的,与模板的工作原理有关如果没有,它是否存在于除C ++之外的任何其他主流语言?我们可以在Java中有一个等价的静态多态吗?C#..任何东西,它会带来什么好处吗?


  5. 最重要的... 使用静态多态性的实际好处是什么?我认为我们可以同意它减少代码灵活性;除了C ++ - 保存一个指针取消引用(虚函数/指针到功能/代理成本)之外,还有哪些优点?什么是类问题,其中静态多态性特别有用,实现的正确选择?



解决方案


  1. 静态多态性行为是 type多数据在编译时发生,而不是运行时间。

  2. 是。

  3. UML是关于类在运行时如何交互 - 不要相信有描述模板的UML格式,但是我可能是错误的。

  4. 据我所知,这是C ++具体的,但是我不是积极的,因为我'没有用过每一种发明的语言。 :)那就是说,像C#和Java这样的JIT语言通常在删除间接调用的性能影响方面非常好,在某些情况下使用在运行时而不是在编译时收集的信息。无论是在编译时还是在编译时都是这样的,毕竟,它被称为即时编译器

  5. 主要的好处是简单的表现。运行时多态可以做任何静态多态可以做的事情(实际上它可以做更多),但它承担间接调用的成本(如果有足够的'em)可能是昂贵的

现在,模板本身在实现编译时多态之外有许多用途 - 例如,使用 boost :: bind 工作当然不是多态的 - 只是为了平滑语言本身的不一致性。


I have some questions about the concept of static polymporhism I somethimes hear about; you may interpret them primarily in the context of C++, but I'd appreciate language-agnostic answers where applicable (hence tagging both C++ and language-agnostic).

  1. How do we define static polymorphism in general? As an example, I believe that the std::sort function from C++ shall be considered statically polymorphic as it depends on some interface provided by some objects which behave like iterators, and the exact behaviour under the interface of provided iterators can be determined in the compile-time. Is this explanation how we define static polymorphism, or is it just a description of a specific case and there's more to it?

  2. What are the common code patterns of using static polymorphism in C++? Also: Is SP only achieved via templates in C++?

  3. Is it true that a given UML class diagram doesn't directly describe how polymorphism is handled and, thus, it can be at least partially implemented either statically or dynamically? In other words: Is the choice of static vs dynamic polymorphism independent of the OOP model, and thus up to the implementor to decide?

  4. Is static polymorphism only C++-specific and related to how templates work? If not, is it present in any other mainstream languages besides C++? Can we have an equivalent of static polymorphism in Java, C#.. anything, and will it bring any benefits?

  5. The most important... What are the actual benefits of using static polymorphism? I think we can agree that it reduces code flexibility; what are the advantages, besides - in the case of C++ - saving one pointer dereference (virtual function / pointer-to-function / delegate cost)? What is the class of problems where static polymorphism is especially useful, the right choice for implementation?

解决方案

  1. Static polymorphic behavior is type polymorphism that occurs at compile time rather than run time.
  2. Yes.
  3. UML is about how classes interact at runtime -- I don't believe there's a UML format for describing templates, but I could be wrong.
  4. As far as I am aware it is C++ specific, but I'm not positive given that I've not used every language ever invented. :) That said, JIT'd languages like C# and Java often are very good at removing the performance impact of indirect calls in some cases using information gleaned at runtime rather than at compile time. Whether this is at compile time or not is kind of up on the air though... after all, it is called a Just-In-Time Compiler.
  5. The main benefit is simply performance. Runtime polymorphism can do everything static polymorphism can do (in fact it can do more), but it carries the cost of indirect calls (which can be expensive if there are enough of 'em)

Now, templates themselves have many uses beyond achieving compile time polymorphism -- for example the SFINAE magic that makes boost::bind work is certainly not polymorphic -- it's merely there in order to smooth over inconsistencies in the language itself.

这篇关于静态多态定义和实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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