Function Templates
In this lesson, we'll explore function templates in detail.
We'll cover the following
Function Templates #
A function template is defined by the keyword template
followed by type or non-type parameters in front of a concrete function. After that, replace the concrete types or non-types with the type or non-type parameters in the function.
- The keywords
class
ortypename
declare the parameters. - The name
T
is usually used for the first parameter. - The parameters can be used in the body of the function.
Passing Arguments in Function Templates #
In the given code snippet, we’ll look at how we can call the initialized variables with our template. Looking at line 2, the function arguments x
and y
in the function xchg
must have the same type. By providing two type parameters like in line 5, the types of arguments can be different. In line 9, we see a non-type template parameter N
.
Get hands-on with 1400+ tech skills courses.