When to use template vs inheritance

C++TemplatesInheritance

C++ Problem Overview


I've been looking around for this one, and the common response to this seems to be along the lines of "they are unrelated, and one can't be substituted for the other". But say you're in an interview and get asked "When would you use a template instead of inheritance and vice versa?"

C++ Solutions


Solution 1 - C++

The way I see it is that templates and inheritance are literally orthogonal concepts: Inheritance is "vertical" and goes down, from the abstract to the more and more concrete. A shape, a triange, an equilateral triangle.

Templates on the other hand are "horizontal" and define parallel instances of code that knowns nothing of each other. Sorting integers is formally the same as sorting doubles and sorting strings, but those are three entirely different functions. They all "look" the same from afar, but they have nothing to do with each other.

Inheritance provides runtime abstraction. Templates are code generation tools.

Because the concepts are orthogonal, they may happily be used together to work towards a common goal. My favourite example of this is type erasure, in which the type-erasing container contains a virtual base pointer to an implementation class, but there are arbitrarily many concrete implementations that are generated by a template derived class. Template code generation serves to fill an inheritance hierarchy. Magic.

Solution 2 - C++

The "common response" is wrong. In "Effective C++," Scott Meyers says in Item 41:

> Item 41: Understand implicit interfaces and compile-time polymorphism.

Meyers goes on to summarize:

> - Both classes and templates support interfaces and polymorphism. > - For classes, interfaces are explicit and centered on function > signatures. Polymorphism occurs at runtime through virtual functions. > - For template parameters, interfaces are implicit and based on valid > expressions. Polymorphism occurs during compilation through template > instantiation and function overloading resolution.

Solution 3 - C++

use a template in a base (or composition) when you want to retain type safety or would like to avoid virtual dispatch.

Solution 4 - C++

Templates are appropriate when defining an interface that works on multiple types of unrelated objects. Templates make perfect sense for container classes where its necessary generalize the objects in the container, yet retain type information.

In case of inheritance, all parameters must be of the defined parameter type, or extend from it. So when methods operate on object that correctly have a direct hierarchical relationship, inheritance is the best choice.

When inheritance is incorrectly applied, then it requires creating overly complex class hierarchies, of unrelated objects. The complexity of the code will increase for a small gain. If this is the case, then use templates.

Solution 5 - C++

Template describes an algorithm like decide the result of a comparison between the two objects of a class or sorting them. The type(class) of objects being operated on vary but the operation or logic or step etc is logically the same.

Inheritance on the other hand is used by the child class only to extend or make more specific the parents functionality. Hope that makes sense

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrent212View Question on Stackoverflow
Solution 1 - C++Kerrek SBView Answer on Stackoverflow
Solution 2 - C++GnawmeView Answer on Stackoverflow
Solution 3 - C++justinView Answer on Stackoverflow
Solution 4 - C++cmcgintyView Answer on Stackoverflow
Solution 5 - C++Judayle L DsouzaView Answer on Stackoverflow