C++ Function Overloading
Give one operation compatible argument shapes and avoid conversions that conceal different units.
How it works
Overloading declares several functions with the same name but different parameter lists. The compiler chooses a viable best match using argument types and conversion rules. A return type alone cannot distinguish overloads. Keep overloads conceptually related: a count and a distance both stored as int should not share a misleading signature with different units. Named types can make those domains distinct. Prefer an obvious exact match over a set that accepts many surprising implicit conversions.
Run it locally
Use a C++20 compiler and matching standard library in a separate lab folder. The command uses c++; substitute g++, clang++ or zig c++ for your toolchain. C++ does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
c++ -std=c++20 -Wall -Wextra -Wpedantic -Werror overloads.cpp -o lessonOn Windows use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Thread examples may need -pthread on your platform.
Download the complete C++ source. Run file labs in a private empty directory, away from existing reports.
Example
#include <iostream>
#include <string>
std::string label(int number) { return "Round-" + std::to_string(number); }
std::string label(const std::string& name) { return "Round: " + name; }
int main() {
std::cout << label(7) << '\n';
std::cout << label(std::string{"Evening"}) << '\n';
}
Expected result
Round-7
Round: Evening
The first argument selects the int form and the explicit string selects the text form. Both return owning strings, so the result remains valid through insertion into the output stream. Defaults and implicit conversions can make a larger overload set ambiguous; reduce the interface instead of forcing callers to guess casts.
Consult the WG21 C++ working group and the C++ Core Guidelines for language references and design guidance. Check the selected standard version and your implementation’s library support.