Function Overloading and Function Templates Flashcards
Function Overloading
allows us to create multiple function with the same name, so long as they have a different set of parameter inputs or types
How does the compiler match a call to an overloaded function?
(called Overload Resolution)
First it will look for an exact match
If this cannot be found it will favour numeric promotion
If this cannot be found it will do numeric conversion
Ambiguous match
when the compiler finds two or more functions that can match a call to an overloaded function. This will cause an error
where must a default argument be placed
must always be the rightmost parameter
Function template
allows us to create a function like definition that serves as a pattern for creating related functions
Type template parameters
placeholders for types we want to be specified later.
Used in function templates
e.g. template < typename T >
Template argument deduction
Define.
What don’t they do.
allows the compiler to deduce the actual type that should be used to instantiate a function from the arguments of the function call
Template argument deduction does not do type conversions
Generic types / Generic programming
Another name for template types and programming using templates
abbreviated function template
(Give code example)
using auto as the parameter type
auto max(auto x, auto y)
{
return (x < y) ? y : x;
}
non-type template parameter
a template parameter with a fixed type that serves as a placeholder for a constexpr value passed in as a template argument.
template <int> // declare a non-type template parameter of type int named N
void print()
{
std::cout << N << '\n'; // use value of N here
}</int>
int main()
{
print<5>(); // 5 is our non-type template argument
return 0; }