How to Use a Template Function Ultimate++

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@wyn·
0.000 HBD
How to Use a Template Function Ultimate++
#### What Will I Learn?
A guide to using templates and template functions aimed at C++ programmers and C programmers moving from C to C++

#### Requirements
[Ultimate++](https://www.ultimatepp.org/)

#### Difficulty
- Intermediate


#### Tutorial Contents
How to Use a Template Function Ultimate++. General Templates, Template Functions Specifically for C Programmers

Being able to define generic functions that can operate on any type is vital in efficient programming. For those just starting in C++, or C programmers trying to understand some of the advantages that C++ offers, templates provide a useful starting point.

The C++ language offers two principal template mechanisms:

- Function Templates;
- Class Templates.

This tutorial looks at the first of these mechanisms.

# How to Use a Template Function Ultimate++

#### Function Templates

A function template is a Ultimate++ mechanism that we can use to define a generic function that is type-inspecific. In other words, when we define the function, we do not specify what types are passed to it, nor what types are returned from it, so long as these are types that we wish to replace with specific ones when we write the code that uses the function.

However, we may define types for any parameters or return values that are type-specific : such as a generic comparison function that returns a Boolean value of true or false.



So, we can define a function, for example, that takes one or more parameters, and only specify their presence, not the types that should be associated with them. A generic example is as follows:
```
template <class T>
T Maximum ( T one, T two )
{
if ( one > two )
return one;
return two;
}
```
<br>
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516277299/jetmogwvbtmaqs3bj47p.png)

<br>
The reader will note that in the first line, we tell the compiler that this is a template, and that we wish to be able to specialize it with a single type, defined by the placeholder, T. This could, in fact, be any value we like, T is just a common convention.

We might also have more than one placeholder in the first line, however each one must have a different name.

Next, we define the function name, and any return values, as we would any other function, except that we only ever talk in terms of the placeholders that we put on the first line. So, this function returns type T, and takes two parameters, each of type T.

The rest of the function definition follows a similar pattern - while we might have other types and operations in the code, whenever we wish to use one that is a template parameter, we use the appropriate placeholder. All the types must be compatible, or have appropriate overloads (in the case of user defined functions).

#### Compile on Demand

When we want to use the code that we have defined, we use the following form:
```
int nFirst = 10;
int nSecond = 100;
int nMax = GetMax ( nFirst, nSecond );
float fFirst, fSecond;
fFirst = 10;
fSecond = 1;
float fMax = GetMax ( fFirst, fSecond );
```
<br>
![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516277429/jpvcjncn3ae59scvzkyo.png)
<br>
The reader will note that we do not even need to specify the type that we want the compiler to use - it will substitute them correctly, and complain if it finds an error. Each usage is compiled on demand, as and when the compiler encounters the references in the code.

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@wyn/how-to-use-a-template-function-ultimate">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , , , , , , ,