257 lines
16 KiB
Markdown
257 lines
16 KiB
Markdown
[over.match.call]
|
||
|
||
# 12 Overloading [[over]](./#over)
|
||
|
||
## 12.2 Overload resolution [[over.match]](over.match#call)
|
||
|
||
### 12.2.2 Candidate functions and argument lists [[over.match.funcs]](over.match.funcs#over.match.call)
|
||
|
||
#### 12.2.2.2 Function call syntax [over.match.call]
|
||
|
||
#### [12.2.2.2.1](#general) General [[over.match.call.general]](over.match.call.general)
|
||
|
||
[1](#general-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L362)
|
||
|
||
In a [function call](expr.call "7.6.1.3 Function call [expr.call]")
|
||
|
||
[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") ( [*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]")opt )
|
||
|
||
if the [*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") names at least one function or
|
||
function template,
|
||
overload resolution is applied as specified in [[over.call.func]](#over.call.func "12.2.2.2.2 Call to designated function")[.](#general-1.sentence-1)
|
||
|
||
If the [*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") denotes an object of class type, overload
|
||
resolution is applied as specified in [[over.call.object]](#over.call.object "12.2.2.2.3 Call to object of class type")[.](#general-1.sentence-2)
|
||
|
||
[2](#general-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L373)
|
||
|
||
If the [*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") is the address of an overload set,
|
||
overload resolution is applied using that set as described above[.](#general-2.sentence-1)
|
||
|
||
[*Note [1](#general-note-1)*:
|
||
|
||
No implied object argument is added in this case[.](#general-2.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
If the function selected by overload resolution is
|
||
an implicit object member function,
|
||
the program is ill-formed[.](#general-2.sentence-3)
|
||
|
||
[*Note [2](#general-note-2)*:
|
||
|
||
The resolution of the address of an
|
||
overload set in other contexts is described in [[over.over]](over.over "12.3 Address of an overload set")[.](#general-2.sentence-4)
|
||
|
||
â *end note*]
|
||
|
||
#### [12.2.2.2.2](#over.call.func) Call to designated function [[over.call.func]](over.call.func)
|
||
|
||
[1](#over.call.func-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L389)
|
||
|
||
Of interest in [[over.call.func]](#over.call.func "12.2.2.2.2 Call to designated function") are only those function calls
|
||
in which the [*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") ultimately contains
|
||
an [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") or [*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]") that designates one or more functions[.](#over.call.func-1.sentence-1)
|
||
|
||
Such a[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]"),
|
||
perhaps nested arbitrarily deep in
|
||
parentheses, has one of the following forms:
|
||
|
||
[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]"):
|
||
[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") . [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]")
|
||
[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") . [*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]")
|
||
[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") -> [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]")
|
||
[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") -> [*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]")
|
||
[*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]")
|
||
[*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]")
|
||
|
||
These represent two syntactic subcategories of function calls:
|
||
qualified function calls and unqualified function calls[.](#over.call.func-1.sentence-3)
|
||
|
||
[2](#over.call.func-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L411)
|
||
|
||
In qualified function calls,
|
||
the function is designated by
|
||
an [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") or [*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]") E preceded by an -> or . operator[.](#over.call.func-2.sentence-1)
|
||
|
||
Since the
|
||
constructA->B is generally equivalent to(*A).B,
|
||
the rest of[[over]](over "12 Overloading") assumes, without loss of generality, that all member
|
||
function calls have been normalized to the form that uses an
|
||
object and the. operator[.](#over.call.func-2.sentence-2)
|
||
|
||
Furthermore, [[over]](over "12 Overloading") assumes that
|
||
the[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") that is the left operand of the. operator
|
||
has type âcv Tâ
|
||
whereT denotes a class[.](#over.call.func-2.sentence-3)[100](#footnote-100 "Note that cv-qualifiers on the type of objects are significant in overload resolution for both glvalue and class prvalue objects.")
|
||
|
||
The set of candidate functions either
|
||
is the set found by name lookup ([[class.member.lookup]](class.member.lookup "6.5.2 Member name lookup"))
|
||
if E is an [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") or
|
||
is the set determined as specified in [[expr.prim.splice]](expr.prim.splice "7.5.9 Expression splicing") if E is a [*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]")[.](#over.call.func-2.sentence-4)
|
||
|
||
The argument list is the[*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]") in the call augmented by the addition of the left operand of
|
||
the. operator in the normalized member function call as the
|
||
implied object argument ([[over.match.funcs]](over.match.funcs "12.2.2 Candidate functions and argument lists"))[.](#over.call.func-2.sentence-5)
|
||
|
||
[3](#over.call.func-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L456)
|
||
|
||
In unqualified function calls, the function is designated by
|
||
an [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") or a [*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]") E[.](#over.call.func-3.sentence-1)
|
||
|
||
The set of candidate functions either
|
||
is the set found by name lookup ([[basic.lookup]](basic.lookup "6.5 Name lookup"))
|
||
if E is an [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") or
|
||
is the set determined as specified in [[expr.prim.splice]](expr.prim.splice "7.5.9 Expression splicing") if E is a [*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]")[.](#over.call.func-3.sentence-2)
|
||
|
||
The set of candidate functions
|
||
consists either entirely of non-member functions or entirely of
|
||
member functions of some classT[.](#over.call.func-3.sentence-3)
|
||
|
||
In the former case or
|
||
if E is either a [*splice-expression*](expr.prim.splice#nt:splice-expression "7.5.9 Expression splicing [expr.prim.splice]") or
|
||
the address of an overload set,
|
||
the argument list is
|
||
the same as the[*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]") in the call[.](#over.call.func-3.sentence-4)
|
||
|
||
Otherwise, the argument list is the[*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]") in the call augmented by the addition of an implied object
|
||
argument as in a qualified function call[.](#over.call.func-3.sentence-5)
|
||
|
||
If the current class is, or is derived from, T, and the keywordthis ([[expr.prim.this]](expr.prim.this "7.5.3 This")) refers to it,
|
||
|
||
- [(3.1)](#over.call.func-3.1)
|
||
|
||
if the unqualified function call
|
||
appears in a precondition assertion of a constructor
|
||
or a postcondition assertion of a destructor
|
||
and overload resolution selects a non-static member function,
|
||
the call is ill-formed;
|
||
|
||
- [(3.2)](#over.call.func-3.2)
|
||
|
||
otherwise,
|
||
the implied object argument is(*this)[.](#over.call.func-3.sentence-6)
|
||
|
||
Otherwise,
|
||
|
||
- [(3.3)](#over.call.func-3.3)
|
||
|
||
if overload resolution selects a non-static member function,
|
||
the call is ill-formed;
|
||
|
||
- [(3.4)](#over.call.func-3.4)
|
||
|
||
otherwise,
|
||
a contrived object of typeT becomes the implied object argument[.](#over.call.func-3.sentence-7)[101](#footnote-101 "An implied object argument is contrived to correspond to the implicit object parameter attributed to member functions during overload resolution. It is not used in the call to the selected function. Since the member functions all have the same implicit object parameter, the contrived object will not be the cause to select or reject a function.")
|
||
|
||
[*Example [1](#over.call.func-example-1)*: struct C {bool a(); void b() { a(); // OK, (*this).a()}void c(this const C&); // #1void c() &; // #2static void c(int = 0); // #3void d() { c(); // error: ambiguous between #2 and #3(C::c)(); // error: as above(&(C::c))(); // error: cannot resolve address of overloaded this->C::c ([[over.over]](over.over "12.3 Address of an overload set"))(&C::c)(C{}); // selects #1(&C::c)(*this); // error: selects #2, and is ill-formed ([[over.match.call.general]](#general "12.2.2.2.1 General"))(&C::c)(); // selects #3}void f(this const C&); void g() const { f(); // OK, (*this).f() f(*this); // error: no viable candidate for (*this).f(*this)this->f(); // OK}static void h() { f(); // error: contrived object argument, but overload resolution// picked a non-static member function f(C{}); // error: no viable candidate C{}.f(); // OK}void k(this int); operator int() const; void m(this const C& c) { c.k(); // OK} C() pre(a()) // error: implied this in constructor precondition pre(this->a()) // OK post(a()); // OK~C() pre(a()) // OK post(a()) // error: implied this in destructor postcondition post(this->a()); // OK}; â *end example*]
|
||
|
||
[100)](#footnote-100)[100)](#footnoteref-100)
|
||
|
||
Note that cv-qualifiers on the type of objects are
|
||
significant in overload
|
||
resolution for
|
||
both glvalue and class prvalue objects[.](#footnote-100.sentence-1)
|
||
|
||
[101)](#footnote-101)[101)](#footnoteref-101)
|
||
|
||
An implied object argument is contrived to
|
||
correspond to the implicit object
|
||
parameter attributed to member functions during overload resolution[.](#footnote-101.sentence-1)
|
||
|
||
It is not
|
||
used in
|
||
the call to the selected function[.](#footnote-101.sentence-2)
|
||
|
||
Since the member functions all have the
|
||
same implicit
|
||
object parameter, the contrived object will not be the cause to select or
|
||
reject a
|
||
function[.](#footnote-101.sentence-3)
|
||
|
||
#### [12.2.2.2.3](#over.call.object) Call to object of class type [[over.call.object]](over.call.object)
|
||
|
||
[1](#over.call.object-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L573)
|
||
|
||
If the [*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1 General [expr.post.general]") E in the function call syntax evaluates
|
||
to a class object of type âcv Tâ,
|
||
then the set of candidate functions
|
||
includes at least the function call operators of T[.](#over.call.object-1.sentence-1)
|
||
|
||
The function call operators of T are the results of a search for the name operator() in the scope of T[.](#over.call.object-1.sentence-2)
|
||
|
||
[2](#over.call.object-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L583)
|
||
|
||
In addition, for each non-explicit conversion function declared in T of the
|
||
form
|
||
|
||
operator [*conversion-type-id*](class.conv.fct#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]") ( ) [*cv-qualifier-seq*](dcl.decl.general#nt:cv-qualifier-seq "9.3.1 General [dcl.decl.general]")opt [*ref-qualifier*](dcl.decl.general#nt:ref-qualifier "9.3.1 General [dcl.decl.general]")opt [*noexcept-specifier*](except.spec#nt:noexcept-specifier "14.5 Exception specifications [except.spec]")opt [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")opt ;
|
||
|
||
where the optional[*cv-qualifier-seq*](dcl.decl.general#nt:cv-qualifier-seq "9.3.1 General [dcl.decl.general]") is the same cv-qualification as, or a greater cv-qualification than,cv,
|
||
and where[*conversion-type-id*](class.conv.fct#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]") denotes the type âpointer to function
|
||
of (P1,â¦,Pn) returning Râ,
|
||
or the type âreference to pointer to function
|
||
of (P1,â¦,Pn) returning Râ,
|
||
or the type
|
||
âreference to function of (P1,â¦,Pn)
|
||
returning Râ, a [*surrogate call function*](#def:surrogate_call_function "12.2.2.2.3 Call to object of class type [over.call.object]") with the unique name*call-function* and having the form
|
||
|
||
R *call-function* ( [*conversion-type-id*](class.conv.fct#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]") F, P1 a1, …, Pn an) { return F (a1, …, an); }
|
||
|
||
is also considered as a candidate function[.](#over.call.object-2.sentence-1)
|
||
|
||
Similarly, surrogate
|
||
call functions are added to the set of candidate functions for
|
||
each non-explicit conversion function declared in a base class ofT provided the function is not hidden withinT by another
|
||
intervening declaration[.](#over.call.object-2.sentence-2)[102](#footnote-102 "Note that this construction can yield candidate call functions that cannot be differentiated one from the other by overload resolution because they have identical declarations or differ only in their return type. The call will be ambiguous if overload resolution cannot select a match to the call that is uniquely better than such undifferentiable functions.")
|
||
|
||
[3](#over.call.object-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L629)
|
||
|
||
The argument list submitted to overload resolution consists of
|
||
the argument expressions present in the function call syntax
|
||
preceded by the implied object argument(E)[.](#over.call.object-3.sentence-1)
|
||
|
||
[*Note [1](#over.call.object-note-1)*:
|
||
|
||
When comparing the
|
||
call against the function call operators, the implied object
|
||
argument is compared against the object parameter of the
|
||
function call operator[.](#over.call.object-3.sentence-2)
|
||
|
||
When comparing the call against a
|
||
surrogate call function, the implied object argument is compared
|
||
against the first parameter of the surrogate call function[.](#over.call.object-3.sentence-3)
|
||
|
||
â *end note*]
|
||
|
||
[*Example [1](#over.call.object-example-1)*: int f1(int);int f2(float);typedef int (*fp1)(int);typedef int (*fp2)(float);struct A {operator fp1() { return f1; }operator fp2() { return f2; }} a;int i = a(1); // calls f1 via pointer returned from conversion function â *end example*]
|
||
|
||
[102)](#footnote-102)[102)](#footnoteref-102)
|
||
|
||
Note that this construction can yield
|
||
candidate call functions that cannot be
|
||
differentiated one from the other by overload resolution because they have
|
||
identical
|
||
declarations or differ only in their return type[.](#footnote-102.sentence-1)
|
||
|
||
The call will be ambiguous
|
||
if overload
|
||
resolution cannot select a match to the call that is uniquely better than such
|
||
undifferentiable functions[.](#footnote-102.sentence-2)
|