Cannot Use std::bind

I have a class called SoundThreadManager.

It defines a function:
1
2
	void Init(f_GetSoundSample getSoundSample, f_SoundConverted playSound, int sampleRate, int sampleSize);
	


and a function pointer:
1
2
        typedef byte*(__stdcall* f_GetSoundSample) (byte*, int);
        


I also have a class called 'OSystem_Cli', which has a function

1
2
	byte* mixCallback(byte* samples, int sampleSize);
        


I need to pass this member function to the Init function of SoundThreadManager.

I attempted to use a std::bind to create such a function pointer like this:

 
auto fn1 = &std::bind(&OSystem_Cli::mixCallback, *_gSystemCli, std::placeholders::_1, std::placeholders::_2);


However I get this error in a file called xmemory:
Error C2661 'std::tuple<OSystem_Cli,std::_Ph<1>,std::_Ph<2>>::tuple': no overloaded function takes 3 arguments CLIScumm C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.25.28610\include\xmemory 1358

I also tried:

 
auto fn1 = &std::bind(&OSystem_Cli::mixCallback, _gSystemCli, std::placeholders::_1, std::placeholders::_2);


But I get this error:
Error C2664 'std::_Binder<std::_Unforced,byte *(__thiscall OSystem_Cli::* )(byte *,int),OSystem_Cli *&,const std::_Ph<1> &,const std::_Ph<2> &> std::bind<byte*(__thiscall OSystem_Cli::* )(byte *,int),OSystem_Cli*&,const std::_Ph<1>&,const std::_Ph<2>&>(_Fx &&,OSystem_Cli *&,const std::_Ph<1> &,const std::_Ph<2> &)': cannot convert argument 2 from 'OSystem_Cli *' to 'OSystem_Cli *&' CLIScumm C:\ScummVMNew\ScummVMWeb\GIT\dists\CLIScumm\Wrapper.cpp 112

Note: _gSystemCli is a pointer to a type of OSystem_Cli.

I followed this example: https://stackoverflow.com/questions/37636373/how-stdbind-works-with-member-functions
This is not the cause of your error, but std::bind() returns a function object, which is incompatible with C function pointers.

std::bind() is obsolete. Just use a lambda and std::function:
1
2
3
std::function<byte *(byte *, int)> f = [&_gSystemCli](byte *x, int y){
    return _gSystemCli->mixCallback(x, y);
};
But, again, this will simply not work with function pointers. You will have to figure out some other way to provide the callback. Did you define Init() or is it a function in some library?
I defined Init.
Then accept an std::function<byte *(byte *, int)> instead of a function pointer.
Your right. My last post should have said "I defined Init, so I will accept a std::function", which is what I did.

Back to my initial error though; although I don't have an example on hand I sometimes use C libraries which take function pointers as arguments. I would love to know why bind did not work, because next time I call such a function I can pass in a member function to it.
Obviously you cannot pass bound functions or member functions to C. C has no such concepts, so the code you're trying to call doesn't know how to use such objects.
What is usually done in C is add a "user data" parameter to the function pointer signature, which is typically a generic pointer. For example,
 
typedef byte *(callback_t*)(void *user_data, byte *, int);
Or sometimes the callback received a struct which itself contains the user data:
1
2
3
4
5
6
7
8
9
struct LibraryContext{
    //...
    void *user_data;
};

byte *callback(LibraryContext *ctx, byte *, int){
    auto user_data = ctx->user_data;
    //...
}
Thanks for you help. This is a great forum, for noobs like me to ask questions.

I will now resolve the question.
Topic archived. No new replies allowed.