Function overloading
Overloading lets several functions share a name if their parameter lists differ. The compiler picks one at the call site by comparing argument types to each candidate. The return type is not part of that choice.
Detailed Explanation
You want log to accept a string, an int, or a pair of both. In C you would write log_str, log_int, log_both. C++ lets you write log three times. The compiler records three different symbols (name mangling encodes the parameters) and, at each call, runs overload resolution to pick one.
Two functions overload when they have the same name and live in the same scope, and their parameter lists differ in type, count, or const/ref qualification of a parameter. void f(int) and void f(double) overload. void f(int) and int f(int) do not: same parameters, different return type. That pair is a redefinition error.
Resolution has a ranked list of conversions. Exact match wins. Then promotion (short to int, float to double). Then standard conversions (int to double, derived pointer to base pointer). Then user-defined conversions (constructors and conversion operators). Then ellipsis. If two candidates land in the same rank, the call is ambiguous and does not compile. If a template and a non-template are otherwise equal, the non-template wins.
const on a member function is part of the parameter list (this is const T* versus T*). That is how std::vector::operator[] can return T& on a mutable vector and const T& on a const one.
Default arguments interact badly with overloading if you are not careful. void f(int n = 0) and void f() are two candidates for f(). Some compilers accept this, some treat it as ambiguous at the call. Do not rely on it. Give the functions different names or drop the default.
Overload resolution happens before the body is instantiated. A candidate can be selected and then fail to compile inside, which is why SFINAE and concepts exist: they remove a candidate before the ranking, instead of letting it win and explode.
When you should overload: the same idea, different types (std::sqrt for float and double; std::begin for arrays and containers). When you should not: unrelated operations that happen to share an English verb. open(filename) and open(door) in the same scope will confuse readers and resolution.
A using-declaration can pull base-class overloads into a derived class so they participate together. Without it, a derived f(double) hides a base f(int) by name, and f(3) will not call the base. That is name hiding, not overload resolution across scopes.
Code Examples
#include <iostream>
#include <string>
void log(const std::string& msg) {
std::cout << msg << '\n';
}
void log(int code) {
std::cout << "code=" << code << '\n';
}
void log(const std::string& msg, int code) {
std::cout << msg << " code=" << code << '\n';
}
int main() {
log("boot"); // prints: boot
log(42); // prints: code=42
log("fail", 500); // prints: fail code=500
}#include <iostream>
struct Counter {
int n = 0;
int get() { return n++; } // mutable: increments
int get() const { return n; } // const: observe only
};
int main() {
Counter c;
const Counter& view = c;
std::cout << c.get() << '\n'; // prints: 0 (then n is 1)
std::cout << view.get() << '\n'; // prints: 1
}Common Gotchas
Ambiguous conversion
`void f(int); void f(double); f(0L);` can be ambiguous because `long` converts to both. Add a `long` overload or cast at the call.
Derived methods hide base overloads
declaring `void f(double)` in the derived class hides `void Base::f(int)` even though the signatures differ. Write `using Base::f;` if you want both.
Return type never selects the overload
`int f(); void f();` is ill-formed. The compiler would not know which `f()` you meant at a discarded-value call.
`NULL` vs `nullptr`
`NULL` is an integer `0` on many platforms, so `f(int)` wins over `f(void*)`. Pass `nullptr`.