Function pointer pitfalls

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 14 10:42:34 PDT 2017


On Tue, Mar 14, 2017 at 05:05:10PM +0000, Inquie via Digitalmars-d-learn wrote:
> I am generating member function pointers using the declaration
> specified from a standard member function. The standard member
> function is a valid D function that could use any types.
> 
> Is there any pitfalls like there are in C++ from generating a function
> pointer from them?
> 
> e.g.,
> 
> X foo(A,B,C) @R @S @T -> X function(A,B,C) @R @S @T fooptr;
> 
> In my case, there are no attributes, so that might ease the burden.
> 
> e.g., a template that converts a member function declaration.
> 
> ToFunctionPtr!("X foo(A,B,C) @R @S @T)", fooptr)
> 
> or
> 
> ToFunctionPtr!(foo, fooptr)
> 
> gives function pointer declaration who's declaration is the same as
> foo.

Not 100% sure what exactly you mean... but I'm guessing you have some
aggregate X with some member function method(), and you want to get a
function pointer from that? Perhaps something like this?

	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;


T

-- 
Turning your clock 15 minutes ahead won't cure lateness---you're just making time go faster!


More information about the Digitalmars-d-learn mailing list