From C to C++ - templates

When moving from C to C++, templates are one of the biggest novelties we encounter.

Generic programming using templates

Templates allow for generic programming - programming for any type. Templates are mainly used to create:

  • Function templates
  • Class templates

What should we learn about templates?

We should learn about the following topics first:

  • Basic function template declaration
  • Basic class template declaration
  • Template specialization

The entire C++ Standard Library is implemented through templates, and this enables us to have robust containers and algorithms that can hold/operate on almost any data type.

If a template is not instantiated, it does not get compiled into a binary file.

Template meta-programming in C++ is widely used in general software development and embedded systems programming.

Posted on January 28, 2025, by

Slobodan Dmitrovic

Slobodan Dmitrovic is a software development consultant, C++ trainer, and author of several books on C and C++.

Contact Slobodan for C++ training and consultancy services.