Why I get delegate when passing address of function?

Ali Çehreli acehreli at yahoo.com
Sun Sep 11 16:26:10 UTC 2022


On 9/11/22 02:54, Injeckt wrote:

 > And what I should do to pass non-static function?

You can combine your class object with other arguments and your thread 
function will know how to unwrap your class object to call its member 
function:

import std.stdio;

// I am not on Windows, so I am making it non-D another way
// Let's assume this is what the library wants from us
alias MyThreadFunc = uint function(void*);

// Let's assume this is what the library provides
extern (C)
void libraryCreateThread(MyThreadFunc func, void* param) {
     writeln("The libraryCreateThread is starting our function");
     func(param);
}

// This is my class
class C {
     string s;

     // This is the function I want to be called
     void myClientThread(void* param) {
         writeln("The desired member function is here");
         writeln("the member: ", s);

         // At this point we know the extra argument shoud be an
         // int (that's why main used one)
         auto arg = *cast(int*)param;
         writeln("the extra argument: ", arg);
     }
}

// We have to play along with what the library wants
// It wants a 'function', so here is one:
// (This could be a static member function)
uint myNonMemberThreadFunc(void* param) {
     writeln("Our function is making the param useful...");

     // See MyThreadArgs below
     auto args = *cast(MyThreadArgs*)param;

     writeln("... and calling our member function");
     args.c.myClientThread(args.extraArg);

     return 0;
}

// This combines a class instance (which is a pointer behind the scene)
// and any other argument
struct MyThreadArgs {
     C c;
     void* extraArg;
}

void main() {
     auto c = new C();
     c.s = "the class member";

     // Assuming some extra argument
     int mainArg = 42;

     // Combine with the class object; this may have to be on the heap
     auto args = MyThreadArgs(c, &mainArg);

     // Do what the library wants
     libraryCreateThread(&myNonMemberThreadFunc, cast(void*)&args);
}

 > Error: function `_server.Server.ClientThread(void* param)` is not
 > callable using argument types `()`. too few arguments, expected `1`, got
 > `0`

That looks like the same problem you had a couple of days ago: The name 
of the function is not a function pointer in D but is a call to it:

- foo: The same thing as foo()
- &foo: 'function'

Ali



More information about the Digitalmars-d-learn mailing list