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

What Is A Function Template In C++?

What Is A Function Template In C++

One of the great features of modern C++ is templates. A template is a simple and a very powerful statement in C++ which defines the operations of a class or function and lets the user apply the same template on different types in those operations. In this post we will explain function templates in C++ which can be used by a professional C++ Compiler and IDE with C++ examples from this article.

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?

For example, here is a function template that adds two a and b parameters in T type,

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

and in the next lines same template can be used for the x and y float variables as below too,

Templates are powerful entity that can be parameterized by one or more template parameters. These parameters can be type template parameters, non-type template parameters, and template template parameters.

What are function templates in C++?

A Template Function may have multiple template parameter types, and the function can still use regular non-templated types.

In a previous post about Function Overloading, we had two add() functions working with integer and float numbers, in that example we can say add() function is overloaded with int and float parameter types, and with the same body.

This add() function can be overloaded for a lot of types, and it could make sense for all of them to have the same body. For cases such as this, C++ can define functions with generic types, known as Function Templates. Defining a function template follows the same syntax as a regular function, except that it is preceded by the template keyword and a series of template parameters enclosed with < and > angle brackets.

Here is the syntax of a function template,

Consider a function max(x, y) that returns the larger of its two arguments. x and y can be of any type that has the ability to be ordered. But, since C++ is a strongly typed language, it expects the types of the parameters x and y to be declared at compile time. Without using templates, many overloaded versions of max are required, one for each data type to be supported even though the code for each version is essentially identical. Each version compares the arguments and returns the larger.

One way around this problem is to use a macro:

The template parameters are a series of parameters separated by commas. These parameters can be generic template types by specifying either the class or type name keyword followed by an identifier. This identifier can then be used in the function declaration as if it was a regular type. For example, in general usage, our previous add() function could be defined as,

Is there a full example of function templates in C++?

Templates are a powerful and versatile feature because of variable type can be defined during usage. Template functions may have multiple template parameters, and the function can still use regular non-templated types. Here is the full example how to use these template functions,

What are non-type template arguments in function templates in C++?

In some cases, the template arguments may include expressions of a particular type instead of being introduced by class or type name,

Here, the second parameter of this add() function template is of type int. It just looks like a regular function parameter, and can actually be used just like one.

The symbol vector must always be accompanied by a data type in angle brackets. It cannot appear alone, except in some cases in the original template definition.

What Is A Function Template 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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++