Generic Programming in C++

C++ Generic Programming Introduction

Using C++ templates, the generic programming pattern generalizes the approach so that it may be used with a variety of data types. Instead of specifying the actual data type, we supply a placeholder in templates, and that placeholder is substituted by the data type that was utilized during compilation. As a result, the compiler would generate 3 copies of the function if the template function were called for an integer, character, and float. Because C++ uses static types, this is feasible.

Ways to use a Template

Let's briefly review standard programming in order to comprehend the use of templates. The C++ programming language requires that all data be kept in a container. These are referred to technically as data types, along the lines of int, float, user-defined, etc.

The process to calculate the distance between two coordinates will stay the same regardless of whether the coordinates are provided as integer or floating-point integers, as we are aware that the technique may be the same for multiple forms of data. The int data type cannot store float values, and using the float data type for int values is a memory waste, hence in C++ programming we must write separate methods for each data type. Therefore, templates in C++ offer a generic solution to this problem.

Overview

The C++ data type agnostic piece of code is written using templates. At the time of compilation, a process known as instantiation of code causes the given placeholder in the code to be substituted by the real data type. Generics refers to the class or function that is defined as a template, while in C++, the term "generic programming" refers to the complete idea.

Functions having generic types that can change their behavior depending on the data type provided during the function call are written using function templates. This makes it simpler for us to carry out the same action without code duplication on several data kinds.

Functions having generic types that can change their behavior depending on the data type provided during the function call are written using function templates. This makes it simpler for us to carry out the same action without code duplication on several data kinds.

OUTPUT:

5
4.5
???..
Process executed in 0.11 seconds
Press any key to continue.

Explanation

The data type was supplied with the function call itself in the example above, but this step might be avoided because the compiler would figure it out based on the values we pass to the function.

Generic Data Type

Classes or functions that have parameterization over a type are known as generic types. Using C++ templates, this is accomplished. A generic data type is built using the template parameters. We supply some template arguments, and the function gets them as type values, which is a similar idea to function parameters.

Below is syntax:

Either typename or the class keyword can be used. Both strategies are equivalent.

The same file Must Contain Both the Declaration and the Definition

There are no typical functions in the templates. They are only ever compiled during an instance with certain template inputs. With the supplied arguments and template, the compiler generates the precise functionality. It is not possible to separate the definition and declaration of the template functions into two files because templates are only compiled when necessary.

Overloading Function Templates

Overloading of the template functions is possible. If a certain function call occurs inside the program, the compiler will look for the precise definition of the overloaded function. The overloaded function will run if it is discovered. In the absence of that, the matching template function will run. Additionally, the compiler will throw an error if the function call is not accompanied by a template function.

OUTPUT:

Inside Template 5.9
Inside Overload 10
???????????..
Process executed in 0.11 seconds
Press any key to continue.

Explanation

  • Calling sum(2.1,3.8) will cause the compiler to look for the exact function, which doesn't exist, then look for the template function, which does exist, so that it may be performed.
  • There is an overload function for the exact data type for a different function call, sum(5,5). The template function won't be invoked as a result.

Template Recursive Functions

Recursion can be accomplished using template functions, and from the perspective of program execution, everything functions just like a regular recursive function.

Templates for Functions with User-Defined Types

Any type that we pass inside the function template's template parameters is up to us to decide. The implication is that user-defined types can also be used to create function templates.

OUTPUT:

Vikram Sharma is 21 years old.
???????????..
Process executed in 0.11 seconds
Press any key to continue.

Explanation:

  • Base is a user-defined type that we have built. It has a function toString() { [native code] } function, a function Object() { [native code] }, and a few private entities.
  • There is then a function template that can be used to print the data by calling it with the appropriate user-defined data type.
  • The copy of the template function with the specific data type will be created when the compiler parses the call printDataofbase(b1), and this call will be calling that copy of the template function where Person replaces T.

Class Templates

The class may also employ templates, just like function templates, to make it compatible with other data types. You may have created a dynamic array using the vector in C++ programming, and you can see that it functions flawlessly with any data type you pass inside the >, such as vector < int >. The class template is solely to blame for this.

Class Template and Friend Function

A friend function is a non-member function that is defined outside of the class and has access to the class's protected and private members. These are employed to connect classes and functions. In the class template, we can specify whether our friend function is a template or a regular function.

Class Template and Static Function

In C++, classes may include either static or non-static variables (instance). The class's objects each contain non-static variables. However, because the static variable is constant across all objects, it is shared by all the new objects as well. One thing to keep in mind while we talk about C++ templates is that the static variable in template classes continues to be shared by all objects of the same type.