C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

Learn How To Eliminate Pointers In Templates in C++

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.

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:

for example, if a and b are int variables, this template can be used as below:

In the next lines the same template can be used for the x and y float variables as shown below:

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,

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++.

If you need more details about Class Templates you can check this official docwiki

Learn How To Eliminate Pointers In Templates in C++ the C++ Builder Logo

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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What is the negation (std::negation) metafunction in C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is Multiset (std::multiset) In Modern C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is Set (std::set) In Modern C++?

C++C++11C++14C++17C++20IteratorsLearn C++

What Is Deque (std::deque) In Modern C++?