Try this:

extern(C) void foo() {}
extern(C)
{
    alias void function() FooFunc;
    // alias typeof(foo) FooFunc;  // or try this one if it works
}

void bar(FooFunc func) { }

void main() {
  bar( &foo );
}

I've had a nasty bug where I forgot to put extern(C) on a function
type like that. I spend the entire day trying to debug the damn thing,
because for some reason calling C code worked, but C code trying to
call a delegate which I've passed failed with arbitrary types.
Basically, it was a communication mismatch with the calling
convention.



On 12/30/10, David Nadlinger <s...@klickverbot.at> wrote:
> As easily verified e.g. by compiling
> ---
> extern(C) void foo() {}
> pragma( msg, typeof( &foo ) );
> ---,
> function pointers types include the linkage type (the code above prints
> »void C function()«).
>
> However, there is no way to specify the linkage type e.g. in the
> signature of a function accepting a delegate, i.e.:
> ---
> extern(C) void foo() {}
> void bar( void function() func ) {} // How to correctly specify the full
> parameter type here?
>
> void main() {
>     bar( &foo );
> }
> ---
>
> This problem is currently somewhat hidden by the fact that DMD simply
> ignores the linkage when doing type checking. But e.g. LDC does strict
> type checking (probably because it needs to reflect the pointer types in
> the LLVM IR, but that's just guessing) today, and it will hopefully be
> added to DMD at some point.
>
> Note: Simply adding »extern( C )« to the type specification does not
> work, but: At the first glance, I couldn't even find any section in the
> language spec for both D1 and D2 mentioning linkage annotations for
> function pointers. Has this part simply not been spec'd yet?
>
> In any case, this currently breaks passing of function pointers to C
> functions resp. their SWIG-generated wrappers with LDC, as I could find
> no easy way to work around it – creating an alias for the function
> pointer type before might work, as extern( C ) seems to be accepted
> there, but this is a major annoyance if you are automatically generating
> code.
>
>
> David
>

Reply via email to