Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dyamic vs Static Polymorphism in C++ : which is preferable?

I understand that dynamic/static polymorphism depends on the application design and requirements. However, is it advisable to ALWAYS choose static polymorphism over dynamic if possible? In particular, I can see the following 2 design choice in my application, both of which seem to be advised against:

  1. Implement Static polymorphism using CRTP: No vtable lookup overhead while still providing an interface in form of template base class. But, uses a Lot of switch and static_cast to access the correct class/method, which is hazardous

  2. Dynamic Polymorphism: Implement interfaces (pure virtual classes), associating lookup cost for even trivial functions like accessors/mutators

My application is very time critical, so am in favor of static polymorphism. But need to know if using too many static_cast is an indication of poor design, and how to avoid that without incurring latency.

EDIT: Thanks for the insight. Taking a specific case, which of these is a better approach?

class IMessage_Type_1
{
  virtual long getQuantity() =0;
...
}
class Message_Type_1_Impl: public IMessage_Type_1
{
  long getQuantity() { return _qty;}
...
}

OR

template <class T>
class TMessage_Type_1
{
  long getQuantity() { return static_cast<T*>(this)->getQuantity(); }
...
}
class Message_Type_1_Impl: public TMessage_Type_1<Message_Type_1_Impl>
{
  long getQuantity() { return _qty; }
...
}

Note that there are several mutators/accessors in each class, and I do need to specify an interface in my application. In static polymorphism, I switch just once - to get the message type. However, in dynamic polymorphism, I am using virtual functions for EACH method call. Doesnt that make it a case to use static poly? I believe static_cast in CRTP is quite safe and no performance penalty (compile time bound) ?

like image 760
vid Avatar asked Mar 28 '12 11:03

vid


People also ask

What is the advantage of dynamic polymorphism?

Dynamic Polymorphism allows Java to support overriding of methods which is central for run-time polymorphism. It allows a class to specify methods that will be common to all of its derivatives while allowing subclasses to define the specific implementation of some or all of those methods.

What is the difference between static polymorphism and dynamic polymorphism in C#?

Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

Are templates example of static polymorphism?

Academic definitionOverloaded functions and templates provide static (compile-time) polymorphism.

Is an example of static polymorphism whereas is an example of dynamic polymorphism?

Static Polymorphism in Java is a type of polymorphism that collects the information for calling a method at compilation time, whereas Dynamic Polymorphism is a type of polymorphism that collects the information for calling a method at runtime. It relates to method overloading. It relates to method overriding.


2 Answers

Static and dynamic polymorphism are designed to solve different problems, so there are rarely cases where both would be appropriate. In such cases, dynamic polymorphism will result in a more flexible and easier to manage design. But most of the time, the choice will be obvious, for other reasons.

One rough categorisation of the two: virtual functions allow different implementations for a common interface; templates allow different interfaces for a common implementation.

like image 102
James Kanze Avatar answered Oct 18 '22 18:10

James Kanze


A switch is nothing more than a sequence of jumps that -after optimized- becomes a jump to an address looked-up by a table. Exactly like a virtual function call is.

If you have to jump depending on a type, you must first select the type. If the selection cannot be done at compile time (essentially because it depends on the input) you must always perform two operation: select & jump. The syntactic tool you use to select doesn't change the performance, since optimize the same.

In fact you are reinventing the v-table.

like image 28
Emilio Garavaglia Avatar answered Oct 18 '22 17:10

Emilio Garavaglia