The C++ language is a very rich and flexible programming language packed full of features designed to help you create modern, robust applications of all types. One of the great features of modern C++ is templates. In some cases, we need to eliminate pointers in templates. In this article, we will explain how to eliminate pointers in templates with C++ examples that can be used in any C++ developer app.
Table of Contents
What is a template in C++?
A template is a very powerful statement in C++ which simply defines the operations of a class, a function, an alias or a variable and lets the user apply the same template on different data types in those template operations. Templates are similar to macros in C++ except the compiler checks the types used before the template is expanded.
How does a template in C++ work?
To help explain templates, here is a function template that adds two a and b parameters in the T type:
1 2 3 4 5 6 7 |
template <class T> T add (T a, T b) { return a+b; } |
for example, if a
and b
are int
variables, this template can be used as below:
1 2 3 |
int i = add <int> (a, b); |
In the next lines the same template can be used for the x
and y
float variables as shown below:
1 2 3 |
float f = add <float> (x, y); |
Templates are very useful, and they are the base of generic programming that allows writing code in a way that is independent of any particular type. A template is an entity which generates an ordinary type or a function at compile time based on arguments the user supplies for the template parameters.
How to eliminate pointers in templates in C++?
Another design technique is to include actual objects, making pointers unnecessary. This can also reduce the number of virtual function calls required, since the compiler knows the actual types of the objects. This is beneficial if the virtual functions are small enough to be effectively inlined. It’s difficult to inline virtual functions when called through pointers, because the compiler doesn’t know the actual types of the objects being pointed to.
Here is an example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
template <class T> aBase { . . . private: T buffer; }; class anObject : public aSubject, public aBase<aFilebuf> { . . . }; |
All the functions in aBase
can call functions defined in aFilebuf
directly, without having to go through a pointer. If any of the functions in aFilebuf
can be inlined, you’ll get a speed improvement, because templates allow them to be inlined.
Is there an example of how to eliminate pointers in templates in C++?
Here is a very simple C++ example of how to eliminate pointers in templates in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> template <class T> class aBase { private: T buffer; }; class aSubject { }; class aFilebuf { }; class anObject : public aSubject, public aBase<aFilebuf> { }; int main() { } |
If you need more details about Class Templates you can check this official docwiki
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here