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

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

问题描述

我有一些关于静态多元性的概念的问题,我有时候会听到;你可以主要在C ++的上下文中解释它们,但是在适用的情况下我会赞赏与语言无关的答案(,因此标记C ++和语言无关的)。


  1. 我们如何定义静态多态性一般?例如,我相信<$ c $来自C ++的c> std :: sort 函数应该被认为是静态多态的,因为它依赖于一些对象提供的某些接口,这些对象的行为类似于迭代器在所提供的迭代器的接口下可以在编译时确定。


  2. 常见的是什么?代码模式在C ++中使用静态多态性?此外:仅通过C ++中的模板实现SP 吗?

  3. 是否真的因此,它可以至少部分地被静态地或动态地实现?换句话说:静态与动态多态性的选择是否与OOP模型无关,从而可以由实现者决定?


  4. 多态性只有C ++ - 特定,并与模板如何工作相关?如果不是,除了C ++,它是否存在于任何其他主流语言中?


  5. 最重要的是... 使用静态多态性的实际好处是什么?我认为我们可以同意,它降低了代码的灵活性;除了 - 在C ++的情况下,保存一个指针解引用(虚函数/指针到函数/委托代价)有什么优点?什么是问题类,其中静态多态性是特别有用的,是实现的正确选择?



解决方案


  1. 静态多态行为是类型多态性

  2. 是的。

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

  4. 据我所知,它是C ++特定的,但我不是肯定的,因为我没有使用每种语言发明。 :)也就是说,JIT的语言(如C#和Java)通常在使用运行时而不是编译时收集的信息来消除间接调用对性能的影响。无论这是在编译时是否是在空气中,虽然...毕竟,它被称为即时编译器

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

现在,模板本身有许多用途,超越实现编译时多态性 - 例如SFINAE魔术,使 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天全站免登陆