静态多态定义与实现
我对静态多态性的概念有一些疑问,我有时听说;您可能主要在 C++ 的上下文中解释它们,但我希望在适用的情况下提供与语言无关的答案(因此标记 C++ 和语言无关).
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).
我们如何定义静态多态? 作为一个例子,我相信
std::sort
函数来自C++ 应该被认为是静态多态的,因为它依赖于一些对象提供的接口,这些对象行为类似于迭代器,并且提供的迭代器接口下的确切行为可以在编译时.这个解释是我们如何定义静态多态,还是只是对特定情况的描述,还有更多内容?
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?
在 C++ 中使用静态多态的常见代码模式有哪些?另外:SP only 是通过 C++ 中的模板实现的吗?
What are the common code patterns of using static polymorphism in C++? Also: Is SP only achieved via templates in C++?
给定的 UML 类图是否直接描述了如何处理多态性,因此,它至少可以部分实现静态的还是动态的?换句话说:静态多态还是动态多态的选择是否独立于 OOP 模型,从而由实现者决定?
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?
静态多态是否只是 C++ 特有的并且与模板的工作方式有关?如果没有,除了 C++ 之外,它是否存在于任何其他主流语言中?我们可以在 Java、C# 中使用静态多态等价物吗?它会带来什么好处吗?
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?
最重要的... 使用静态多态的实际好处是什么?我想我们可以同意它降低了代码的灵活性;除了 - 在 C++ 的情况下 - 节省一个指针取消引用(虚拟函数/函数指针/委托成本)之外,还有什么优点?什么是问题类别,其中静态多态特别有用,是实现的正确选择?
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?
推荐答案
- 静态多态行为是在编译时而非运行时发生的类型多态.莉>
- 是的.
- UML 是关于类在运行时如何交互的――我不相信有一种 UML 格式来描述模板,但我可能是错的.
- 据我所知,它是特定于 C++ 的,但鉴于我没有使用过每一种发明的语言,我并不乐观.:) 也就是说,像 C# 和 Java 这样的 JIT 语言通常非常擅长在某些情况下使用在运行时而不是在编译时收集的信息来消除间接调用的性能影响.这是否是在编译时,虽然有点悬而未决……毕竟,它被称为实时编译器.
- 主要的好处就是性能.运行时多态可以做静态多态可以做的所有事情(实际上它可以做更多),但它带来了间接调用的成本(如果有足够多的话,这可能会很昂贵)
现在,模板本身除了实现编译时多态之外还有很多用途――例如,使 boost::bind
工作的 SFINAE 魔法当然不是多态的――它只是为了平滑语言本身的不一致.
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.
相关文章