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

Learn to Use Function Templates in C++

A Template is a simple and a very powerful statement 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 a generic form of a template usage,

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,

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,

Non-type Template Arguments

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

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.

Get started building powerful apps with C++Builder!

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.

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++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?

Worth reading...
Learn About Void and Return Type in Functions in C++