C++ Logo

std-proposals

Advanced search

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

From: ஜெய்கணேஷ் குமரன் <jaiganesh.kumaran_at_[hidden]>
Date: Tue, 6 Feb 2024 18:44:11 +0000

On 06-Feb-2024, at 23:40, Arthur O'Dwyer via Std-Proposals <std-proposals_at_[hidden]> wrote:

On Tue, Feb 6, 2024 at 12:56 PM Jason McKesson via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> wrote:
On Tue, Feb 6, 2024 at 12:08 PM ஜெய்கணேஷ் குமரன்
<jaiganesh.kumaran_at_[hidden]<mailto: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.)

No, I don't want something like bind; I just want the member-function-pointer-taking syntax to give me the this pointer as well so that it is cleaner to construct a wrapper if needed - otherwise you can throw it away by assigning into a regular member-function pointer variable, which is the same as what you get in a static context.

mem_fn_wrapper(&class_name::function); // the wrapper should be able to extract both the function pointer and the this pointer of the current instance.

void (class_name::* p)() = &class_name::function; // only the function pointer - no this type.

I don't want a callable wrapper built in to the language; I just want the syntax to expose a structure with the this pointer as a member so you can extract it if necessary.

Now this does mean you have to change the type of the expression. However, to preserve backwards compatibility in most cases, you may choose to make auto and decltype decay it into a regular member-function pointer type, whilst still being of a different type.

std::mem_fn_with_this_ptr p = &class_name::function; // special language type (template).
std::mem_fn_wrapper w = p; // library type (template); along with the member-function pointer, also stores the this pointer which is provided by p, and exposes operator() with a signature that does not require passing the this pointer.

Received on 2024-02-06 18:44:19