Overloading
A guide to Clause 12: how functions with the same name coexist, how a call picks exactly one of them, and the rules for operator overloading and user-defined literals.
Key points
- Functions with the same name in the same scope can coexist as long as their parameter lists and related properties differ; this is overloading. Functions with the same name in different scopes hide each other; that isn't overloading.
- When a call names an overloaded function, the compiler selects a function in three steps: list the candidate functions, filter out the viable functions, and use implicit conversion sequences to find the unique best one.
- Overload resolution looks only at types. Access and
= deleteare checked after the selection; if the selected function fails them, the program is ill-formed, and resolution doesn't fall back to the runner-up. - Suggested reading order: [over.pre] → [over.match] → [over.match.viable] → [over.best.ics] → [over.ics.rank] → [over.match.best], then [over.match.oper] for operators.
C has no overloading, so the absolute value function needs three names: abs, labs, fabs. C++ lets all of them be called abs and has the compiler choose based on the argument types. Clause 12 specifies that choice: which declarations with the same name can coexist, and which one a given call actually calls.
int abs(int x) { return x < 0 ? -x : x; }
double abs(double x) { return x < 0 ? -x : x; }
int main() {
auto a = abs(1); // abs(int): int → int needs no conversion
auto b = abs(1.0); // abs(double)
static_assert(sizeof(a) == sizeof(int) && sizeof(b) == sizeof(double));
}Which declarations can coexist#
[over.pre] defines overloading. Whether two functions with the same name can coexist is decided in the current draft by the notion of correspondence from [basic.scope.scope]: two declarations whose parameter-type-lists (after adjustments such as array-to-pointer and dropping top-level const), constraints and object parameters are the same correspond; they declare the same function and don't overload each other. So two declarations that differ only in their return type can't coexist.
How one function is selected#
Overload resolution ([over.match]) chooses among the functions found by name lookup, in three steps:
| Step | What it does | Subclauses |
|---|---|---|
| Candidate functions | List the competing functions according to the context of the call (ordinary call, operator, constructor, conversion function, etc.) | [over.match.funcs], [over.match.oper] |
| Viable functions | The number of arguments fits, constraints are satisfied, and each argument can be implicitly converted to its parameter | [over.match.viable] |
| Best viable function | Compare the conversions argument by argument, then apply a series of tie-breakers | [over.match.best] |
Conversions are compared by their implicit conversion sequence: the steps that convert an argument to the parameter type. [over.best.ics] defines what conversions such a sequence consists of, and [over.ics.rank] specifies how two sequences are compared; for example, no conversion is better than an integral promotion, and an integral promotion is better than other conversions.
Access control, whether the function is deleted, and so on are checked only after the selection; see [over.match]/3. Successful overload resolution doesn't mean the call is valid.
Operators and the rest#
| Subclause | Contents |
|---|---|
| [over.match.class.deduct] | Class template argument deduction: one round of overload resolution over the deduction guides |
| [over.over] | Taking the address of an overloaded function: &f selects by target type |
| [over.oper] | Which operators can be overloaded, and in what form they are declared |
| [over.built] | The built-in operator candidates imagined for the purpose of overload resolution |
| [over.literal] | User-defined literals operator""_km |
Since C++20, a < b can be rewritten as (a <=> b) < 0 and a != b as !(a == b); these rewritten candidates are covered in [over.match.oper], and defaulted == and <=> in [class.compare].
Prerequisites#
Overload resolution only chooses among the results of name lookup ([basic.lookup], [basic.lookup.argdep]). A function that lookup didn't find is invisible to overload resolution. Implicit conversion sequences consist of standard conversions ([conv]) and user-defined conversions ([class.conv]), and essentially model copy-initializing the parameter from the argument ([dcl.init]). A function template first goes through template argument deduction ([temp.deduct]) to produce a specialization, which then enters overload resolution.
Overload resolution walkthrough: eight classic puzzles works several classic overload resolution problems from start to finish with per-argument conversion tables; it makes good practice after reading this clause.
Contents
- 12.1Preamble[over.pre]done
- 12.2Overload resolution[over.match]done
- 12.2.1General[over.match.general]
- 12.2.2Candidate functions and argument lists[over.match.funcs]done
- 12.2.2.1General[over.match.funcs.general]
- 12.2.2.2Function call syntax[over.match.call]
- 12.2.2.3Operators in expressions[over.match.oper]done
- 12.2.2.4Initialization by constructor[over.match.ctor]
- 12.2.2.5Copy-initialization of class by user-defined conversion[over.match.copy]
- 12.2.2.6Initialization by conversion function[over.match.conv]
- 12.2.2.7Initialization by conversion function for direct reference binding[over.match.ref]
- 12.2.2.8Initialization by list-initialization[over.match.list]
- 12.2.2.9Class template argument deduction[over.match.class.deduct]done
- 12.2.3Viable functions[over.match.viable]done
- 12.2.4Best viable function[over.match.best]done
- 12.2.4.1General[over.match.best.general]
- 12.2.4.2Implicit conversion sequences[over.best.ics]done
- 12.2.4.2.1General[over.best.ics.general]
- 12.2.4.2.2Standard conversion sequences[over.ics.scs]
- 12.2.4.2.3User-defined conversion sequences[over.ics.user]
- 12.2.4.2.4Ellipsis conversion sequences[over.ics.ellipsis]
- 12.2.4.2.5Reference binding[over.ics.ref]
- 12.2.4.2.6List-initialization sequence[over.ics.list]
- 12.2.4.3Ranking implicit conversion sequences[over.ics.rank]done
- 12.3Address of an overload set[over.over]done
- 12.4Overloaded operators[over.oper]draft
- 12.5Built-in operators[over.built]draft
- 12.6User-defined literals[over.literal]draft