Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Templates(generic programming) vs polymorphism?

Tags:

c++

oop

templates

I want to start this question by saying that it is paradigm-related and that I am only trying to clarify some concepts. So I have been programming in Python for about 2 years now, dipped my toes into Java but not too much and I want to delve into C++. I've used it before but not for large projects with a lot of design involved.

When I first started explored it I believed that it addressed OOP similarly to Java where everything has to implement an interface. Then I bumped into the concept of templates which I immediately though to be a workaround to provide polymorphic behaviour to primitives(ints, floats) which did not implement it(basically what Python did through duck-typing and no formal interfaces). But I soon discovered that templates were used to provide the same behaviour to non-primitive types.

So my question is: what reason is there to use classic polymorphism over templates, and what is the general approach to this in the C++ community?

EDIT Just found this which pretty much answers the question(static polymorphism really need to wrap my head around this terminology).

like image 370
nikitautiu Avatar asked Nov 19 '12 16:11

nikitautiu


People also ask

Are templates a form of polymorphism?

Templates provide static/compile time polymorphism. While virtualism provides dynamic polymorphism.

Are templates and generics the same?

Comparing Templates and GenericsGenerics are generic until the types are substituted for them at runtime. Templates are specialized at compile time so they are not still parameterized types at runtime.

Are templates example of static polymorphism?

Operator overloading, Function overloading, and Templates are the mechanisms of static polymorphism. During compile-time, a function and an object are linked, and this process is known as static polymorphism.

Is generic programming polymorphism?

Generic programming allows you to write the data structure using a variable name for the type so that you can specify the type of data for the structure later. Polymorphism is used with collections of objects that are all of a related type, where related means sharing a base class.


1 Answers

At the risk of making sweeping generalizations, templates are mostly used similarly to Generics in Java - they allow you to build a class or function that can be used with many different data types. Take std::list, part of the Standard Template Library. You can make a linked list of integers with std::list<int>, or a list of objects with std::list<MyClass>. Another example is std::thread, which uses templates to take a function (or lambda or functor) and its arguments to run in another thread.

As for choosing between a function f(SomeInterface x) and a function template f(T x), it really depends on context and is somewhat subjective. Some things to take into consideration are:

  • Function templates and class templates are resolved at compile time, so you may get better performance. However,

  • C++ compilers historically generate barely-descipherable garbage for template errors. Clang has done some work to improve this, and other compilers are getting better in an effort to match Clang. Things are getting better, but it's still pretty ugly.

  • Don't be afraid to use traditional polymorphism with interfaces and implementation classes. While templates are used instead of of polymorphism in some cases (see C++'s std::thread which uses templates vs. Java's Thread which uses a Runnable interface), polymorphism is still extremely common in C++ libraries and projects.

In short, feel free to consider using templates, but don't look at them as a replacement for polymorphism. Look at a popular C++ library and you're bound to find plenty of polymorphism. Take OGRE, a popular C++ graphics engine. If you look at its class list, you'll find lots of interfaces (such as WindowEventListener and FrameListener) which the user can derive a class from in order to interact with the library.

like image 133
Matt Kline Avatar answered Oct 16 '22 15:10

Matt Kline