Everyone that works with Template knows how powerful there are. However, many times when I encounter a C programmer that just starting to learn / work with C++ I ran into the same question: Why is Template better than Void*?
I believe the answer is quite simple and I will try to explain it in this post, but first one must understand the fundamental differences between the two methods.
What is Void*?
Void* is a mechanism in the C programming language that allows you to write a general code. I.e. you can write general functions (or structures) that receive (contain) pointer to Voidand actually send whatever type you want to it. Void pointers are often used to allow functions to operate on data of an unknown type.
What is Template?
Template is a mechanism in the C++ programming that allows you to create a collection of functions and containers that operate on typed data! I.e. each individual function / container created from the template is working with a very specific type. The generic of the code is rising from the fact that many different typed functions / containers can be created from the same template.
It is important to understand that the Void* is not a C++ mechanism and the only reason you can use it in C++ is because C++ has a back-support to all C features. However, the fact you can use it does not mean you should! Especially since Template is much better than Void*.
The main differences between Template and Void*
- Using Void* we have only one instance of the code. With Templates, on the other hand, we have several versions of the code, since the compiler creates a separate version of the function for each specified type
Unlike with Templates,
- - The compiler can not perform type checking.
- - We can not perform type specific operations such as operators or inner methods.
- - We can not force two general parameters (or more) to be of the same type.
- - Easier to write. You create only one generic version of your function and there is no need for switch of casting (unlike Void*).
- - Templates provide a straightforward way of abstracting type information.
- - Templates are type-safe. Template is a compiler time mechanism. Therefore, the types are known at compile time and all error are discovered then (unlike with the Void* mechanism that cause run time errors).
The fact that Template is a compiler-time mechanism is one of Templates greatest advantage. However it is also the cause to its disadvantage.
In Templates, the moment we create a specific container from the template for type X – this container will only hold instances of type X. This is unlike with Void* that allows the user to enter completely different types into the same container.
However, if we want to enter different types with a common general idea into the same container we can always go around it by using polymorphism.