C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allow conversion of memfunc pointers to func pointers

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Mon, 8 Jan 2024 10:47:38 +0100
On 07/01/2024 21:19, Frederick Virchanza Gotham via Std-Proposals wrote:
>
> But anyway . . . if we are to retain the class type when calling
> "memfunc_to_func", then we'll have to put the class type in as the
> first parameter, something like:
>
> int (MyClass::*pmf)(double) = &MyClass::SomeMethod;
>
> int (*pf)(MyClass*,double) = std::memfunc_to_func(p);
>
> But "pf" is not intended to be used as a normal function. Instead you must do:
>
> std::invoke_func_as_memfunc(pf, &myvar, 56.7L);

If you have to use a special facility to invoke the
faux-pointer-to-function anyhow, then what's the point of
"memfunc_to_func"? It sounds like a terrible idea, as it would be
completely type unsafe.


If the idea is to handle PF and PMF in a homogeneous way, you can just
use a variant instead?

using PMF = int (MyClass::*)(double) const;
using PF = int (*)(const MyClass &, double);

std::variant<PMF, PF> v = ~~~;

MyClass obj; double d = 3.14;
v.visit([&](auto &&ptr) { std::invoke(ptr, obj, d); });

-- 
Giuseppe D'Angelo

Received on 2024-01-08 09:47:42