Ch13: Function Overloading¶
TL;DR: Function Overloading: C++ allows multiple functions to share a name provided they have distinct parameter lists (differing in type, order, or count).
โก Quick Reference¶
#include <iostream>
#include <string_view>
#include <iterator>
// Overloads distinguished by parameter types
int max(int a, int b) { return (a > b) ? a : b; }
double max(double a, double b) { return (a > b) ? a : b; }
// Overloads distinguished by parameter order
double max(int a, double b) { return (a > b) ? a : b; }
double max(double a, int b) { return (a > b) ? a : b; }
// Overloads distinguished by parameter count
double max(double a, int b, int c) { return a + b + c; }
// Overload accepting string views
std::string_view max(std::string_view a, std::string_view b) { return (a > b) ? a : b; }
// Overloads distinguishing pointer-to-const from mutable pointer
int max(int* a, int* b) { return (*a > *b) ? *a : *b; }
int max(const int* a, const int* b) { return (*a > *b) ? *a : *b; }
// Overloads distinguishing const references from mutable references
int max(int& a, int& b) { return (a > b) ? a : b; }
int max(const int& a, const int& b) { return (a > b) ? a : b; }
int main() {
int arr[] = {1, 2, 3};
size_t size = std::size(arr); // std::size(array) - returns elements count
auto result = max(10, 20); // Deduces return type from selected overload
return 0;
}
๐ง Core Concepts¶
- Function Overloading: C++ allows multiple functions to share a name provided they have distinct parameter lists (differing in type, order, or count).
- Signature Constraints: A function's signature comprises its name and parameter types. The return type is excluded, so functions differing only by return type cannot coexist.
- Overload Resolution & Ambiguity: The compiler selects the best match using argument types. If implicit conversions make multiple overloads equally valid, the compiler rejects the call as ambiguous.
โ ๏ธ Pitfalls (Quick Scan)¶
| Mistake | Fix |
|---|---|
| Defining overloads that differ only by return type | Change parameter count, types, or order to distinguish overloads |
| Creating overloads where implicit conversions have equal rank | Avoid overloads where conversion paths can lead to multiple matching candidates (20.4) |
Adding constness to pointer variables themselves (e.g., int const) to overload |
Overload using pointer-to-const (const int*) instead of const pointer (20.6) |
| Overloading functions that possess default parameters | Avoid mixing default parameters with function overloading (20.8) |
๐ Full Details¶
Cause โ Effect โ Fix with timestamp (click to expand)
* **Defining overloads that differ only by return type** -> **Compile-time error because return type is not part of the function signature** -> **Change parameter count, types, or order to distinguish overloads (01:05)** * **Creating overloads where implicit conversions have equal rank** -> **Compile-time error due to ambiguous function call** -> **Avoid overloads where conversion paths can lead to multiple matching candidates (20.4)** * **Adding constness to pointer variables themselves (e.g., `int* const`) to overload** -> **Compile-time error because top-level const modifiers on parameters do not change signatures** -> **Overload using pointer-to-const (`const int*`) instead of const pointer (20.6)** * **Overloading functions that possess default parameters** -> **Compile-time error due to ambiguity when functions are called without arguments** -> **Avoid mixing default parameters with function overloading (20.8)**๐ Repo Files¶
20.FunctionOverloading/20.2OverloadingWithDifferentParameters/main.cpp20.FunctionOverloading/20.3OverloadingWithPointerParameters/main.cpp20.FunctionOverloading/20.4OverloadingWithReferenceParameters/main.cpp20.FunctionOverloading/20.5OverloadingWithConstParametersByValue/main.cpp20.FunctionOverloading/20.6OverloadingWithConstPointerAndPointerToConstParameters/main.cpp20.FunctionOverloading/20.7OverloadingWithConstReferences/main.cpp20.FunctionOverloading/20.8OverloadsWithDefaultParameters/main.cpp