https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113388
Bug ID: 113388
Summary: Calling explicit object member function without object
argument inside a function that is not an implicit
object member function
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: cooky.ykooc922 at gmail dot com
Target Milestone: ---
According to https://eel.is/c++draft/over.call.func#3:
In unqualified function calls, the function is named by a primary-expression.
The function declarations found by name lookup ([basic.lookup]) constitute the
set of candidate functions. Because of the rules for name lookup, the set of
candidate functions consists either entirely of non-member functions or
entirely of member functions of some class T. In the former case or if the
primary-expression is the address of an overload set, the argument list is the
same as the expression-list in the call. Otherwise, the argument list is the
expression-list in the call augmented by the addition of an implied object
argument as in a qualified function call. If the current class is, or is
derived from, T, and the keyword this ([expr.prim.this]) refers to it, then the
implied object argument is (*this). Otherwise, a contrived object of type T
becomes the implied object argument; if overload resolution selects a
non-static member function, the call is ill-formed.
In other words, the code snippet below must produce an error because a call to
'bar()' inside the function 'foo', 'baz', or any function which is not an
implicit object member function itself, is ill-formed without the object
argument. There is no error inside implicit object member function 'qux'
because the implied object argument is '*this' unlike other explicit object
member functions.
struct A {
void bar(this A) {}
static void foo() {
// not ok: this should be error
// because the overload resolution
// picked a non-static member function
// without an object argument
bar();
// ok [error as expected]:
// bar(A{});
// ok [converted to function pointer]:
(&A::bar)(A{});
// ok [called with an object argument]:
A{}.bar();
}
void baz(this A self) {
// ok
self.bar();
// ok [error as expected]
// self.bar(self);
// ok [error as expected]
// bar(A{});
// not ok: this should be error
// because the overload resolution
// picked a non-static function
// without an object argument
// even inside the scope of the
// explicit object member function
bar();
// ok
A{}.bar();
}
void qux() {
// ok
bar();
// ok [error as expected]
// bar(*this);
}
};
godbolt link with -std=c++23: https://godbolt.org/z/89Msa7Gfj