Templates and its usability in C++

Templates and its usability in C++

March 25, 2019
4 min read
1797 views
0

Templates and its usability in C++

Template is simple and yet very powerful features of C++. This is the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

The idea of template is to pass any data type as a parameter so that we don't need to write the same code for different data types. To a large degree, templated classes and functions are the more focused on the algorithmic thought rather than the specific nuances of a single data type. Template makes classes or functions more reusable and easier to maintain.

Why do need the templates in C++?

Many C++ programs use common data structures like stacks, queues, and lists. A program may require a queue of customers and a queue of messages. One could easily implement a queue of customers, then take the existing code and implement a queue of messages. The program grows, and now there is a need for a queue of orders. So just take the queue of messages and convert that to a queue of orders, There is a need to make some changes to the queue implementation. A very daunting task!

Since the code has been duplicated in many places, the re-inventing source code is not an intelligent approach in an object-oriented environment which encourages re-usability.

So, it seems to make more sense to implement a queue that can contain any arbitrary type rather than duplicating code. That is more commonly referred to as templates.

Templates are very useful when implementing generic constructs like vectors, stacks, lists, queues which can be used with any arbitrary type. C++ templates provide a way to reuse source code as opposed to inheritance and composition which provides a way to reuse object code. C++ templates allow one to implement a generic Queue template that has a type parameter.

So, how does it work?

Like macros, templates are expended at compile time. The difference is, compiler does type checking before template expansion. The idea is simple, source code contains only function or class, but compiled code may contain multiple copies of the same function or class.

The concept of templates can be used in two different ways:

  • Class Templates
  • Function Templates

Class Templates

A class template works in a similar to a normal class, with one key difference. Class templates are useful when a class defines something that is independent of data type. Can be useful for classes like LinkedList, Binary Tree, Stack, Queue, Array, etc.

Normally, you would need to create a different class for each data type OR create different member variables and functions within a single class. This will unnecessarily bloat your code base and will be hard to maintain, as a change is one class/function should be performed on all classes/functions.

Here, you can find an example of a Template class.

Function Templates

Like class templates, function templates can be created for generic operations. A single function template can work with different data types at once but, a single normal function can only work with one set of data types.

Normally, if you need to perform identical operations on two or more types of data, you use function overloading. A template parameter in function work as a special kind of parameter that can be used to pass any data type as argument, just like regular function parameters. It helps to perform the same task writing less and maintainable code.

Here, you can find an example of a Template function.

Within an application, you can instantiate the same template multiple times with the same arguments or with different arguments. If you use the same arguments, the repeated instantiations are redundant. These redundant instantiations increase compilation time, increase the size of the executable, and deliver no benefit.

Was this article helpful?Vote to let the author know
0
Share this article

Loading comments...

Related Articles

Trading platforms compared: which one fits your style

Trading platforms compared: which one fits your style

That's the usual shape of the mistake. People compare platforms by feature count, the way you'd compare phones in a shop, when the useful question is narrower. What do you actually do in a normal week, and which software makes that loop faster and less error prone?

9 views
Read More
Read full article: Trading platforms compared: which one fits your style
Growing Your Business Through Better SEO

Growing Your Business Through Better SEO

SEO requires regular monitoring because search results are constantly changing. Competitors publish new content, websites are updated, search behavior changes, and search engines continue to improve how they evaluate pages.

9 views
Read More
Read full article: Growing Your Business Through Better SEO
DomDetailer: A Practical Guide to Domain Research, Backlink Metrics, and Bulk Analysis

DomDetailer: A Practical Guide to Domain Research, Backlink Metrics, and Bulk Analysis

Discover how DomDetailer simplifies domain research by bringing Moz, Majestic, and backlink data together in one place. Learn how to evaluate expired and auction domains using authority metrics, referring domains, Trust Flow, Citation Flow, topical signals, and a simple one-credit-per-domain pricing model.

11 views
Read More
Read full article: DomDetailer: A Practical Guide to Domain Research, Backlink Metrics, and Bulk Analysis