Callbacks and interfacing with C

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Tue Oct 30 03:57:36 PDT 2012


On Tue, 30 Oct 2012 11:15:55 +0100
Alex Rønne Petersen <alex at lycus.org> wrote:

> On 30-10-2012 11:13, Nick Sabalausky wrote:
> > Ok, a C function pointer like this:
> >
> >      struct MyStruct{
> >          int (*foo)(int);
> >      };
> >
> > Translates to D as this:
> >
> >      struct MyStruct{
> >          int function(int) foo;
> >      }
> >
> > But what about calling conventions? There isn't any "int extern(C)
> > function(int)" is there? Not sure if that would even make sense.
> >
> > So do you just make sure that whatever func you assign to it is an
> > extern(C)? Or something else?
> >
> 
> You generally do it this way:
> 
> alias extern (C) int function(int) MyFn;
> 
> struct MyStruct {
>      MyFn foo;
> }
> 
> This makes sure the calling convention is correct. In general,
> calling convention is part of both the function signature and
> function pointer type - function pointers just default to extern (D).
> 

Hmm, that leads me to another Q:


    extern(C): // <-- Note this
    
    alias int function(int) MyFn;

    struct MyStruct {
        MyFn foo1;
        int function(int) foo2;
    }

    void bar(int function(int) foo3) {...}

Which, if any, of foo1/foo2/foo3 are extern(C)? (I know bar definitely
is.)



More information about the Digitalmars-d-learn mailing list