[over.match.best.general]

# 12 Overloading [[over]](./#over)

## 12.2 Overload resolution [[over.match]](over.match#best.general)

### 12.2.4 Best viable function [[over.match.best]](over.match.best#general)

#### 12.2.4.1 General [over.match.best.general]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L1808)

Define ICSi(F) as
the implicit conversion sequence that converts
the ith argument in the list to the type of
the ith parameter of viable function F[.](#1.sentence-1)

[[over.best.ics]](over.best.ics "12.2.4.2 Implicit conversion sequences") defines the implicit conversion sequences and [[over.ics.rank]](over.ics.rank "12.2.4.3 Ranking implicit conversion sequences") defines what it means for one implicit conversion sequence to be
a better conversion sequence or worse conversion sequence than
another[.](#1.sentence-2)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L1819)

Given these definitions,
a viable function F1 is defined to be a[*better*](#def:overloading,resolution,better_viable_function "12.2.4.1 General [over.match.best.general]") function than another viable function F2 if for all arguments i,ICSi(F1) is not a worse conversion
sequence than ICSi(F2), and then

- [(2.1)](#2.1)

for some argument j,ICSj(F1) is a better conversion sequence thanICSj(F2), or, if not that,

- [(2.2)](#2.2)

the context is an initialization by user-defined conversion
(see [[dcl.init]](dcl.init "9.5 Initializers"),[[over.match.conv]](over.match.conv "12.2.2.6 Initialization by conversion function"), and [[over.match.ref]](over.match.ref "12.2.2.7 Initialization by conversion function for direct reference binding"))
and the standard conversion sequence
from the result of F1 to the destination type
(i.e., the type of the entity being initialized)
is a better conversion sequence than the standard conversion sequence
from the result of F2 to the destination type
  [*Example [1](#example-1)*: struct A { A(); operator int(); operator double();} a;int i = a; // a.operator int() followed by no conversion is better than// a.operator double() followed by a conversion to intfloat x = a; // ambiguous: both possibilities require conversions,// and neither is better than the other â *end example*]
 or, if not that,

- [(2.3)](#2.3)

the context is an initialization by conversion function for [direct
reference binding](over.match.ref "12.2.2.7 Initialization by conversion function for direct reference binding [over.match.ref]") of a reference to function type, the
return type of F1 is the same kind of reference (lvalue or rvalue)
as the reference being initialized, and the return type of F2 is not
  [*Example [2](#example-2)*: template <class T> struct A {operator T&(); // #1operator T&&(); // #2};typedef int Fn();
A<Fn> a;
Fn& lf = a; // calls #1 Fn&& rf = a; // calls #2 â *end example*]
 or, if not that,

- [(2.4)](#2.4)

F1 is not a function template specialization andF2 is a
function template
specialization, or, if not that,

- [(2.5)](#2.5)

F1 andF2 are
function template specializations,
and the function template
forF1 is more specialized than the template forF2 according to the partial ordering rules described in [[temp.func.order]](temp.func.order "13.7.7.3 Partial ordering of function templates"),
or, if not that,

- [(2.6)](#2.6)

F1 and F2 are non-template functions andF1 is more partial-ordering-constrained thanF2 ([[temp.constr.order]](temp.constr.order "13.5.5 Partial ordering by constraints"))
  [*Example [3](#example-3)*: template <typename T = int>struct S {constexpr void f(); // #1constexpr void f(this S&) requires true; // #2};

    void test() { S<> s;
 s.f(); // calls #2} â *end example*]
 or, if not that,

- [(2.7)](#2.7)

F1 is a constructor for a class D,F2 is a constructor for a base class B of D, and
for all arguments
the corresponding parameters of F1 and F2 have the same type
  [*Example [4](#example-4)*: struct A { A(int = 0);};

    struct B: A {using A::A;
 B();};

    int main() { B b; // OK, B​::​B()} â *end example*]
 or, if not that,

- [(2.8)](#2.8)

F2 is a rewritten candidate ([[over.match.oper]](over.match.oper "12.2.2.3 Operators in expressions")) andF1 is not
  [*Example [5](#example-5)*: struct S {friend auto operator<=>(const S&, const S&) = default; // #1friend bool operator<(const S&, const S&); // #2};bool b = S() < S(); // calls #2 â *end example*]
 or, if not that,

- [(2.9)](#2.9)

F1 and F2 are rewritten candidates, andF2 is a synthesized candidate
with reversed order of parameters
and F1 is not
  [*Example [6](#example-6)*: struct S {friend std::weak_ordering operator<=>(const S&, int); // #1friend std::weak_ordering operator<=>(int, const S&); // #2};bool b = 1 < S(); // calls #2 â *end example*]
 or, if not that,

- [(2.10)](#2.10)

F1 and F2 are generated
from class template argument deduction ([[over.match.class.deduct]](over.match.class.deduct "12.2.2.9 Class template argument deduction"))
for a class D, andF2 is generated
from inheriting constructors from a base class of D while F1 is not, and
for each explicit function argument,
the corresponding parameters of F1 and F2 are either both ellipses or have the same type,
or, if not that,

- [(2.11)](#2.11)

F1 is generated from a[*deduction-guide*](temp.deduct.guide#nt:deduction-guide "13.7.2.3 Deduction guides [temp.deduct.guide]") ([[over.match.class.deduct]](over.match.class.deduct "12.2.2.9 Class template argument deduction"))
and F2 is not, or, if not that,

- [(2.12)](#2.12)

F1 is the [copy deduction candidate](over.match.class.deduct#def:copy_deduction_candidate "12.2.2.9 Class template argument deduction [over.match.class.deduct]") and F2 is not, or, if not that,

- [(2.13)](#2.13)

F1 is generated from a non-template constructor
and F2 is generated from a constructor template[.](#2.sentence-1)
  [*Example [7](#example-7)*: template <class T> struct A {using value_type = T;
 A(value_type); // #1 A(const A&); // #2 A(T, T, int); // #3template<class U> A(int, T, U); // #4// #5 is the copy deduction candidate, A(A)};

    A x(1, 2, 3); // uses #3, generated from a non-template constructortemplate <class T> A(T) -> A<T>; // #6, less specialized than #5 A a(42); // uses #6 to deduce A<int> and #1 to initialize A b = a; // uses #5 to deduce A<int> and #2 to initializetemplate <class T> A(A<T>) -> A<A<T>>; // #7, as specialized as #5 A b2 = a; // uses #7 to deduce A<A<int>> and #1 to initialize â *end example*]

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L2022)

If there is exactly one viable function that is a better function
than all other viable functions, then it is the one selected by
overload resolution; otherwise the call is ill-formed[.](#3.sentence-1)[105](#footnote-105 "The algorithm for selecting the best viable function is linear in the number of viable functions. Run a simple tournament to find a function W that is not worse than any opponent it faced. Although it is possible that another function F that W did not face is at least as good as W, F cannot be the best function because at some point in the tournament F encountered another function G such that F was not better than G. Hence, either W is the best function or there is no best function. So, make a second pass over the viable functions to verify that W is better than all other functions.")

[*Example [8](#example-8)*: void Fcn(const int*, short);void Fcn(int*, int);

int i;short s = 0;

void f() { Fcn(&i, s); // is ambiguous because &i â int* is better than &i â const int*// but s â short is also better than s â int Fcn(&i, 1L); // calls Fcn(int*, int), because &i â int* is better than &i â const int*// and 1L â short and 1L â int are indistinguishable Fcn(&i, 'c'); // calls Fcn(int*, int), because &i â int* is better than &i â const int*// and 'c' â int is better than 'c' â short} â *end example*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L2083)

[*Note [1](#note-1)*:

If the best viable function was made viable by one or more default arguments,
additional requirements apply ([[over.match.viable]](over.match.viable "12.2.3 Viable functions"))[.](#4.sentence-1)

â *end note*]

[105)](#footnote-105)[105)](#footnoteref-105)

The algorithm
for selecting the best viable function is linear in the number
of viable
functions[.](#footnote-105.sentence-1)

Run a simple tournament to find a functionW that is not
worse than any
opponent it faced[.](#footnote-105.sentence-2)

Although it is possible that another functionF thatW did not face
is at least as good asW,F cannot be the best function because at some point in the
tournamentF encountered another functionG such thatF was not better thanG[.](#footnote-105.sentence-3)

Hence,
either W is
the best function or there is no best function[.](#footnote-105.sentence-4)

So, make a second pass over
the viable
functions to verify thatW is better than all other functions[.](#footnote-105.sentence-5)
