www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function pointer pitfalls

reply Inquie <Inquie data1.com> writes:
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.
Mar 14
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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!
Mar 14
parent reply Inquie <Inquie data1.com> writes:
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.
Mar 14
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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.
Mar 14
parent Inquie <Inquie data1.com> writes:
On Tuesday, 14 March 2017 at 19:14:34 UTC, H. S. Teoh wrote:
 On Tue, Mar 14, 2017 at 06:59:58PM +0000, Inquie via 
 Digitalmars-d-learn wrote:
 [...]
[...]
 [...]
[...]
 [...]
[...] 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: [...]
Yeah, I don't think I'll run in to that problem since I'm constructing the function ahead of time and only need the declaration but we'll see. Thanks again.
Mar 14