C++ Templates(generic programming) vs polymorphism?
c++, oop, templates
Solution
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.
Problem
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).