Interfacing with C++

Mike Parker aldacron at gmail.com
Sun Feb 4 08:17:31 UTC 2018


On Sunday, 4 February 2018 at 07:54:12 UTC, infinityplusb wrote:
> Hi all
>
> I'm looking to try and write an interface to C++, but given I'm 
> a casual dabbler in D, it's slightly beyond my current ability 
> in terms of both C++ and D!
>
> As a leg up, how would one translate something like this from 
> C++ to D?
>
> `typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* 
> b, void* userdata );`
>
> From my basic understanding I assumed something like:
>
> `alias CvCmpFunc = Typedef!(int) ; `
>
> but then I'm stuck as to where the rest of the parameters would 
> get passed?
> Do I just declare it as a function instead? That wasn't my 
> understanding of reading how typedefs worked, however so I'm a 
> little confused.
>
> Any help would be appreciated.
>

First, you have to understand how CV_CDECL is defined. This is 
going to determine the calling convention that functions pointed 
to by CvCmpFunc will have. Assuming it's defined to cdecl, the 
standard C calling convention (which I'm guess it is) then your D 
definition needs to be extern(C). If it's stdcall, then you need 
extern(Windows). If it's empty, then you need extern(C++).

Second, because this is a function pointer you're declaring, you 
need to use D's function pointer declaration syntax.

Third, we don't need to use the Typedef template for this. alias 
alone is fine.

So assuming CV_CDECL is cdecl, this should do it:

extern(C) alias CvCmpFunc = int function(const(void)*, 
const(void)*, void*);


More information about the Digitalmars-d-learn mailing list