C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Fwd: Simplify taking pointer to non-static member function

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 6 Feb 2024 13:10:02 -0500
On Tue, Feb 6, 2024 at 12:56 PM Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Tue, Feb 6, 2024 at 12:08 PM ஜெய்கணேஷ் குமரன்
> <jaiganesh.kumaran_at_[hidden]> wrote:
> >
> > If you're in a context where `function` results in a non-static,
> > non-overloaded member function, then `&function` will result in a
> > pointer to that function.
> >
> >
> > Unfortunately no, this does not work; you must qualify it with the class
> name.
> >
> > From cppreference:
> > Expressions such as &(C::f) or &f inside C's member function do not form
> pointers to member functions.
>
> Well, the three major compilers disagree:
> https://gcc.godbolt.org/z/ddMbecv84
> It could be bugs in all of them though. It's a pretty narrow corner case.
>

Taking the address of a static member function is fine and gives you a
pointer-to-function.
The forbidden thing, which I think all compilers *do* correctly forbid, is
when you take the address of a non-static member function to get a
pointer-to-member-function:
https://gcc.godbolt.org/z/7KhfdEPKW


> I want something like this:
> > auto f = &class_name::function;
> > class_name* p = f.this_ptr;
>
> That doesn't make any sense. Member pointers are not associated with
> any particular instance. That's kind of the point.
>

+1. This fundamental misunderstanding of what a "pointer to member"
actually is, makes me think that the original feature request was
mis-motivated too.
(OP wants an easier way to extract pointers-to-member-functions... but at
the same time, OP thinks "pointer-to-member-function" means something more
like std::bind. So maybe OP is really looking for C++11 lambda syntax? He's
certainly not looking for pointers-to-member-functions anymore.)

–Arthur

Received on 2024-02-06 18:10:16