Ch6.6: Function Overloading
Overview
C++ allows multiple functions to share the same name as long as their parameter lists differ. This feature is called function overloading.
In this chapter, you will learn:
- what function overloading is
- how overload resolution works
- what counts as a different parameter list
- why return type alone cannot overload a function
- how references and
constaffect overload selection - how ambiguous overloads occur
- how overloading interacts with default arguments
1. Basic function overloading
Two or more functions may have the same name if their parameter types differ.
void print(int x)
{
::fast_io::println("int: ", x);
}
void print(double d)
{
::fast_io::println("double: ", d);
}
int main()
{
print(42); // calls print(int)
print(3.14); // calls print(double)
}
The compiler chooses the correct function based on the argument types.
2. What counts as a different parameter list?
Overloads must differ in their parameter types or number of parameters.
void f(int);
void f(double); // OK — different type
void f(int, int); // OK — different number of parameters
These are all valid overloads.
3. Return type does not participate in overloading
Only the parameter list determines whether two functions are different overloads. The return type is completely ignored for overload resolution.
int g();
double g(); // ❌ error — return type alone does not create a new overload
The compiler must choose an overload before it knows the return type.
auto x = g(); // which g() should this call?
Because the return type cannot disambiguate the call, this overload set is invalid. Only the parameters can differ.
4. Overloading with references and const
References and const qualifiers affect overload resolution.
void h(int & x)
{
::fast_io::print("non-const reference\n");
}
void h(int const & x)
{
::fast_io::print("const reference\n");
}
Calling h with a const value selects the const overload:
int main()
{
int a{10};
int const b{20};
h(a); // calls h(int &)
h(b); // calls h(int const &)
}
5. Ambiguous calls
Sometimes the compiler cannot decide which overload is better.
void k(long);
void k(double);
int main()
{
k(0); // ❌ ambiguous — 0 can convert to long or double
}
When two overloads are equally good, the call is ambiguous.
6. Overloading with default arguments
Default arguments and overloading can interact and sometimes create ambiguities.
void m(int x, int y = 0);
void m(int x);
A call like m(5) is ambiguous:
- the second overload matches directly as
m(int) - the first overload also matches as
m(int, int)using the default value fory
The compiler cannot prefer one over the other, so the call fails to compile.
When designing overloads, make sure that calls cannot match multiple overloads in this way. In many cases, you should choose either:
- a single function with default arguments, or
- a clear set of overloads without defaults on overlapping signatures
Key takeaways
- Function overloading allows multiple functions with the same name.
- Overloads must differ in parameter types or number of parameters.
- Return type alone cannot overload a function.
constand references affect overload resolution.- Ambiguous calls cause compilation errors.
- Default arguments can make overloads ambiguous if signatures overlap.