Files
2025-10-25 03:02:53 +03:00

124 lines
4.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[over.match.general]
# 12 Overloading [[over]](./#over)
## 12.2 Overload resolution [[over.match]](over.match#general)
### 12.2.1 General [over.match.general]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L49)
Overload resolution is a mechanism for selecting the best
function to call given a list of expressions that are to be the
arguments of the call and a set of[*candidate functions*](#def:candidate "12.2.1General[over.match.general]") that can
be called based on the context of the call[.](#1.sentence-1)
The selection
criteria for the best function are the number of arguments, how
well the arguments match the parameter-type-list of the
candidate function,
how well (for non-static member functions) the object
matches the object parameter,
and certain other properties of the candidate function[.](#1.sentence-2)
[*Note [1](#note-1)*:
The function selected by overload resolution is not
guaranteed to be appropriate for the context[.](#1.sentence-3)
Other restrictions,
such as the accessibility of the function, can make its use in
the calling context ill-formed[.](#1.sentence-4)
— *end note*]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L71)
Overload resolution selects the function to call in seven distinct
contexts within the language:
- [(2.1)](#2.1)
invocation of a function named in the [function call syntax](over.call.func "12.2.2.2.2Call to designated function[over.call.func]");
- [(2.2)](#2.2)
invocation of a function call operator, a pointer-to-function
conversion function, a reference-to-pointer-to-function conversion
function, or a reference-to-function
conversion function on a class object named in the function
call syntax ([[over.call.object]](over.call.object "12.2.2.2.3Call to object of class type"));
- [(2.3)](#2.3)
invocation of the operator referenced in an expression ([[over.match.oper]](over.match.oper "12.2.2.3Operators in expressions"));
- [(2.4)](#2.4)
invocation of a constructor for default- or direct-initialization ([[dcl.init]](dcl.init "9.5Initializers"))
of a class object ([[over.match.ctor]](over.match.ctor "12.2.2.4Initialization by constructor"));
- [(2.5)](#2.5)
invocation of a user-defined conversion for[copy-initialization](dcl.init#def:copy-initialization "9.5Initializers[dcl.init]") of a class object ([[over.match.copy]](over.match.copy "12.2.2.5Copy-initialization of class by user-defined conversion"));
- [(2.6)](#2.6)
invocation of a conversion function for initialization of an object of a
non-class type from an expression of class type ([[over.match.conv]](over.match.conv "12.2.2.6Initialization by conversion function")); and
- [(2.7)](#2.7)
invocation of a conversion function for conversion
in which a reference ([[dcl.init.ref]](dcl.init.ref "9.5.4References"))
will be [directly bound](over.match.ref "12.2.2.7Initialization by conversion function for direct reference binding[over.match.ref]")[.](#2.sentence-1)
Each of these contexts defines the set of candidate functions and
the list of arguments in its own unique way[.](#2.sentence-2)
But, once the
candidate functions and argument lists have been identified, the
selection of the best function is the same in all cases:
- [(2.8)](#2.8)
First, a subset of the candidate functions (those that have
the proper number of arguments and meet certain other
conditions) is selected to form a set ofviable functions ([[over.match.viable]](over.match.viable "12.2.3Viable functions"))[.](#2.8.sentence-1)
- [(2.9)](#2.9)
Then the best viable function is selected based on the[implicit conversion sequences](over.best.ics "12.2.4.2Implicit conversion sequences[over.best.ics]") needed to
match each argument to the corresponding parameter of each
viable function[.](#2.9.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L120)
If a best viable function exists and is unique, overload
resolution succeeds and produces it as the result[.](#3.sentence-1)
Otherwise
overload resolution fails and the invocation is ill-formed[.](#3.sentence-2)
When overload resolution succeeds,
and the best viable function is not [accessible](class.access "11.8Member access control[class.access]") in the context
in which it is used,
the program is ill-formed[.](#3.sentence-3)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L130)
Overload resolution results in a [*usable candidate*](#def:candidate,usable "12.2.1General[over.match.general]") if overload resolution succeeds and
the selected candidate
is either not a function ([[over.built]](over.built "12.5Built-in operators")), or
is a function that is not deleted and
is accessible from the context
in which overload resolution was performed[.](#4.sentence-1)