Static polymorphic behavior is type polymorphism that occurs at compile time rather than run time.
Yes.
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.
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.
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.
The best way to understand it using examples. Policy Based Design is one example of static polymorphism. And in my opinion, it's very powerful technique to achieve static polymorphism.
发布评论
评论(3)
现在,模板本身除了实现编译时间之外还有很多用途多态性——例如,使
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.使用示例来理解它的最好方法。 基于策略的设计是静态多态性的一个示例。在我看来,这是实现静态多态性的非常强大的技术。
另一个例子是,奇怪的重复模板模式(CRTP),这也是一种强大的技术。
The best way to understand it using examples. Policy Based Design is one example of static polymorphism. And in my opinion, it's very powerful technique to achieve static polymorphism.
Another example is, Curiously recurring template pattern (CRTP) which is also powerful technique.
https://en.wikipedia.org/wiki/Function_overloading
https://en.wikipedia.org/wiki/Function_overloading