On Tue, Mar 14, 2017 at 06:59:58PM +0000, Inquie via Digitalmars-d-learn wrote:
> On Tuesday, 14 March 2017 at 17:42:34 UTC, H. S. Teoh wrote:
[...]
> >     struct X {
> >             int method(float x) { return 0; }
> >     }
> > 
> >     typeof(&X.method) membptr;
> >     pragma(msg, typeof(membptr)); // prints `int function(float x)`
> > 
> > If you need to refer to the function pointer type frequently, you
> > could alias it to something easier to type;
> > 
> >     alias FuncPtr = typeof(&X.method);
> >     FuncPtr membptr;
[...]
> Thanks, that will work. In C++ there were issues with pointers and one
> would have to properly group the function name or some thing like
> that. Your suggestion avoids all that.
[...]

Keep in mind, though, that the above creates a function pointer with the
same signature as the member function, but you may not be able to assign
a member pointer to it because it lacks object context.  To wit:

----
        struct X {
                int method(float x) { return 0; }
        }

        X x;

        alias FuncPtr = typeof(&X.method);
        FuncPtr fp;

        alias MembPtr = typeof(&x.method);
        MembPtr mp;

        mp = &x.method; // OK
        //fp = &x.method; // NG: cannot implicitly convert expression 
(&x.method) of type int delegate(float z) to int function(float z)
-----

&x.method is a delegate because it encapsulates the instance of X that
it should be invoked with, so you can't assign it to a func ptr without
that context (since method() can't be called without an instance of X).


T

-- 
Question authority. Don't ask why, just do it.

Reply via email to