C++ Logo

std-proposals

Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 4 Feb 2024 15:00:49 -0500
On Sun, Feb 4, 2024 at 2:26 PM ஜெய்கணேஷ் குமரன் via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hello all,
>
> Currently taking a pointer to a non-static member function in C++ is unnecessarily verbose.
>
> auto p = function; // error
> auto p = class_name::function; // error
> auto p = &class_name::function; // OK
>
> Please allow the first two to compile.

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. And if you're not in such a context,
`function` shouldn't work at all. So the only thing you're arguing is
that you shouldn't have to use `&`.

Personally, it was a mistake for C to have functions naturally decay
into function pointers, and it's good that C++ doesn't continue that
beyond C compatibility. `&` is not that hard to write and does not
constitute meaningful verbosity. It also adds clarity, since you don't
have to remember how decaying works.

> Another thing I would like to have is getting a structure containing both the 'this' pointer and the function pointer when taking a member-function pointer while being inside another member function. This would simply lots of event-handling code where you may have to pass in (this, &class_name::handler) currently.

You can write a (template) type that does this easily enough. Though
it wouldn't work with overloading.

> There could also be a wrapper in the standard library that stores any kind of pointer (raw and smart) along with the member-function pointer. I have wished std::mem_fn was this but unfortunately it is not.
>
> Note: I do not have the time to participate in standardisation in order to open formal proposals, but I wish someöne else does based on my ideas.

Received on 2024-02-04 20:01:01