www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Call delegate from C++

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
How do you pass a delegate to a c++ function to be called by it?

The function to pass the delegate to is:

extern (C++) int
fakeEntrypoint(
     extern(C++) void function(void* /*delegate's context*/) func,
     void* /*delegate's context*/ arg);


What I want is:

int entrypoint(scope void delegate() func)
{
     return fakeEntrypoint(
                 // for some reason func.funcptr is void function()
                 cast(void function(void*))func.funcptr,
                 func.ptr);
}

but that fails with

cannot pass argument cast(void function(void*))func.funcptr of 
type
void function(void*) to parameter extern (C++) void 
function(void*) func
Apr 24 2019
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2019-04-24 18:20, Nicholas Wilson wrote:
 How do you pass a delegate to a c++ function to be called by it?
 
 The function to pass the delegate to is:
 
 extern (C++) int
 fakeEntrypoint(
      extern(C++) void function(void* /*delegate's context*/) func,
      void* /*delegate's context*/ arg);
 
 
 What I want is:
 
 int entrypoint(scope void delegate() func)
 {
      return fakeEntrypoint(
                  // for some reason func.funcptr is void
function()
                  cast(void function(void*))func.funcptr,
                  func.ptr);
 }
 
 but that fails with
 
 cannot pass argument cast(void function(void*))func.funcptr of type
 void function(void*) to parameter extern (C++) void function(void*) func
You probably need to include `extern(C++)` in the cast, if that's possible. -- /Jacob Carlborg
Apr 24 2019
prev sibling parent Alex <AJ gmail.com> writes:
On Wednesday, 24 April 2019 at 16:20:17 UTC, Nicholas Wilson 
wrote:
 How do you pass a delegate to a c++ function to be called by it?

 The function to pass the delegate to is:

 extern (C++) int
 fakeEntrypoint(
     extern(C++) void function(void* /*delegate's context*/) 
 func,
     void* /*delegate's context*/ arg);


 What I want is:

 int entrypoint(scope void delegate() func)
 {
     return fakeEntrypoint(
                 // for some reason func.funcptr is void 
 function()
                 cast(void function(void*))func.funcptr,
                 func.ptr);
 }

 but that fails with

 cannot pass argument cast(void function(void*))func.funcptr of 
 type
 void function(void*) to parameter extern (C++) void 
 function(void*) func
It's clear that your delegate is not extern C++ int entrypoint(scope extern(C++) void delegate() func)
Apr 25 2019