Welcome to C++ Program Design!
For academic exchanges, please add WeChat: 22995321
Basics of C++
Classes of C++
C++ Programming and Object-Oriented Design
2.3 Overloads and templates
In C++, two different functions can have the same name if their parameters are different; either because they have a different number of parameters, or because any of their parameters are of a different type. For example:
In this example, there are two functions called
operate, but one of them has two parameters of type
int, while the other has them of type
double. The compiler knows which one to call in each case by examining the types passed as arguments when the function is called. If it is called with two
intarguments, it calls to the function that has two
intparameters, and if it is called with two
doubles, it calls the one with two
In this example, both functions have quite different behaviors, the
intversion multiplies its arguments, while the
doubleversion divides them. This is generally not a good idea. Two functions with the same name are generally expected to have -at least- a similar behavior, but this example demonstrates that is entirely possible for them not to. Two overloaded functions (i.e., two functions with the same name) have entirely different definitions; they are, for all purposes, different functions, that only happen to have the same name.
Note that a function cannot be overloaded only by its return type. At least one of its parameters must have a different type.
Function templatesOverloaded functions may have the same definition. For example:
sumis overloaded with different parameter types, but with the exact same body.
sumcould 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++ has the ability to 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
templatekeyword and a series of template parameters enclosed in angle-brackets <>:
template <template-parameters> function-declaration
The template parameters are a series of parameters separated by commas. These parameters can be generic template types by specifying either the
typenamekeyword followed by an identifier. This identifier can then be used in the function declaration as if it was a regular type. For example, a generic
sumfunction could be defined as:
It makes no difference whether the generic type is specified with keyword
typenamein the template argument list (they are 100% synonyms in template declarations).
In the code above, declaring
SomeType(a generic type within the template parameters enclosed in angle-brackets) allows
SomeTypeto be used anywhere in the function definition, just as any other type; it can be used as the type for parameters, as return type, or to declare new variables of this type. In all cases, it represents a generic type that will be determined on the moment the template is instantiated.
Instantiating a template is applying the template to create a function using particular types or values for its template parameters. This is done by calling the function template, with the same syntax as calling a regular function, but specifying the template arguments enclosed in angle brackets:
name <template-arguments> (function-arguments)
For example, the
sumfunction template defined above can be called with:
sum<int>is just one of the possible instantiations of function template
sum. In this case, by using
intas template argument in the call, the compiler automatically instantiates a version of
sumwhere each occurrence of
SomeTypeis replaced by
int, as if it was defined as:
Let's see an actual example:
In this case, we have used
Tas the template parameter name, instead of
SomeType. It makes no difference, and
Tis actually a quite common template parameter name for generic types.
In the example above, we used the function template
sumtwice. The first time with arguments of type
int, and the second one with arguments of type
double. The compiler has instantiated and then called each time the appropriate version of the function.
Note also how
Tis also used to declare a local variable of that (generic) type within
Therefore, result will be a variable of the same type as the parameters
b, and as the type returned by the function.
In this specific case where the generic type
Tis used as a parameter for
sum, the compiler is even able to deduce the data type automatically without having to explicitly specify it within angle brackets. Therefore, instead of explicitly specifying the template arguments with:
It is possible to instead simply write:
without the type enclosed in angle brackets. Naturally, for that, the type shall be unambiguous. If
sumis called with arguments of different types, the compiler may not be able to deduce the type of
Templates are a powerful and versatile feature. They can have multiple template parameters, and the function can still use regular non-templated types. For example:
x and y are equal
Note that this example uses automatic template parameter deduction in the call to
Is equivalent to:
There is no ambiguity possible because numerical literals are always of a specific type: Unless otherwise specified with a suffix, integer literals always produce values of type
int, and floating-point literals always produce values of type
10has always type
10.0has always type
Non-type template argumentsThe template parameters can not only include types introduced by
typename, but can also include expressions of a particular type:
The second argument of the
fixed_multiplyfunction template is of type
int. It just looks like a regular function parameter, and can actually be used just like one.
But there exists a major difference: the value of template parameters is determined on compile-time to generate a different instantiation of the function
fixed_multiply, and thus the value of that argument is never passed during runtime: The two calls to
mainessentially call two versions of the function: one that always multiplies by two, and one that always multiplies by three. For that same reason, the second template argument needs to be a constant expression (it cannot be passed a variable).