Problems with function as parameter

Josh moonburntm at gmail.com
Fri Sep 22 04:32:08 UTC 2017


On Friday, 22 September 2017 at 03:26:36 UTC, Mike Parker wrote:
> On Friday, 22 September 2017 at 02:22:46 UTC, Josh wrote:
>
>>
>> src\mixer.d(80,22): Error: function pointer 
>> Mix_ChannelFinished (extern (C) void function(int channel)) is 
>> not callable using argument types (extern (C) void 
>> delegate(int channel))
>>
>> Code:
>> void unmuteAfterPlaySound()
>> {
>> 	Mix_ChannelFinished(&channelDone);
>> }
>>
>> extern (C) void channelDone(int channel)
>> {
>> 	unmuteMusic();
>> }
>
> The error message indicates that `channelDone` is a member of a 
> class or a struct. A pointer to a member function is a delegate 
> (or closure), not a function pointer. Free functions, static 
> nested functions, and static member functions all produce 
> function pointer. Non-static member functions and non-static 
> nested functions all produce delegates/closures. See the docs:
>
> https://dlang.org/spec/function.html#closures
>
> If you need to manipulate instance members from a C callback, 
> you'll need a way to implement a mechanism to work out which 
> instance you need.

Perfect, that's the info I needed. As these functions were in a 
class, setting channelDone and unmuteMusic to static worked.

As an aside, in that doc it says "The .funcptr property of a 
delegate will return the function pointer value as a function 
type". So I also tried 
Mix_ChannelFinished((&channelDone).funcptr); and this compiled, 
but caused a segfault when
the callback ran. What would have caused this? Is it because it's 
a C function?


More information about the Digitalmars-d-learn mailing list