public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cooky.ykooc922 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/113388] New: Calling explicit object member function without object argument inside a function that is not an implicit object member function
Date: Sun, 14 Jan 2024 16:51:48 +0000	[thread overview]
Message-ID: <bug-113388-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             reply	other threads:[~2024-01-14 16:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-14 16:51 cooky.ykooc922 at gmail dot com [this message]
2024-01-15  7:04 ` [Bug c++/113388] " waffl3x at protonmail dot com
2024-01-15 10:18 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-113388-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).