Function pointer pitfalls

Inquie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 14 11:59:58 PDT 2017


On Tuesday, 14 March 2017 at 17:42:34 UTC, H. S. Teoh wrote:
> 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

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.




More information about the Digitalmars-d-learn mailing list