I'm trying to call a function that takes an argument, void(*)(void*, int, const char*), but I cannot figure out how to pass those arguments to the function.
Example:
void ptr(int);
int function(int, int, void(*)(int));
I am trying to call the function like this:
function(20, 20, ptr(20));
Is this possible?
You are doing one thing incorrectly - you are trying to invoke your 'ptr' function before invoking 'function'. What you were supposed to do is to pass just a pointer to 'ptr' and invoke 'ptr' using passed pointer from 'function' like that:
void ptr(int x)
{
printf("from ptr [%d]\n", x);
}
int function(int a, int b , void (*func)(int) )
{
printf( "from function a=[%d] b=[%d]\n", a, b );
func(a); // you must invoke function here
return 123;
}
void main()
{
function( 10, 2, &ptr );
// or
function( 20, 2, ptr );
}
which gives:
from function a=[10] b=[2]
from ptr [10]
from function a=[20] b=[2]
from ptr [20]
which is what you wanted
for
function(20, 20, ptr(20));
to work - you would have to have sth like:
// 'ptr' must return sth (int for example)
// if you want its ret val to be passed as arg to 'function'
// this way you do not have to invoke 'ptr' from within 'function'
int ptr(int);
int function(int, int , int);
The usual trick is to use a typedef for signature:
typedef void signature_t (void*, int, const char*);
Notice that without the typedef the syntax is like a function declaration. It declares signature_t as a typedef for functions, so you'll always use pointers to signature_t in practice.
Then you can declare your "high-order" function as
int function (int, int, signature_t*);
See also this reply.
The correct syntax for function call is:
function(20,20, &ptr);
If you feel lost, try some tutorials, or this
Unless I totally misinterpret your code, you are trying to pass a function pointer with an argument by doing
function(20, 20, ptr(20));
That is incorrect and illegal. In order to pass a function as a parameter into another function you have to follow the following syntax
function(20, 20, &ptr);
or
function(20, 20, ptr);
Even though I would recomment leaving the '&' for readability
You can't pass ptr(20) to this function, because you can only pass the pointer to the function but not the pointer with the argument. You may read about functors and theu will help you with such problem. Or the other solution is to change the signature to
int function(int, int, void(*)(void) );
And write function
void ptr_wrap(void) {ptr(20);}
so you can call function(20, 20, ptr_wrap);. But functors can solve this problem in more elegant way.
ptr(20) is the return value of ptr, when you pass 20 to it. If you want to pass the function (and not its return value), you should write just function(20,20,ptr);
Related
I'm trying to understand bind and pre-fill functions in C++.
Here's my example:
#include <iostream>
#include <functional>
#include <vector>
class Voice
{
public:
double mValue;
private:
};
class VoiceManager
{
public:
VoiceManager() { }
~VoiceManager() { }
typedef std::function<void(Voice &)> VoiceChangerFunction;
inline void UpdateVoices(VoiceChangerFunction callback) {
for (int i = 0; i < mNumOfVoices; i++) {
callback(mVoices[i]);
}
}
static void SetValue(Voice &voice, unsigned int value) {
voice.mValue = value;
std::cout << voice.mValue << std::endl;
}
private:
static const int mNumOfVoices = 4;
Voice mVoices[mNumOfVoices];
};
int main()
{
VoiceManager voiceManager;
VoiceManager::VoiceChangerFunction callback;
callback = std::bind(&VoiceManager::SetValue, std::placeholders::_1, 100);
voiceManager.UpdateVoices(callback);
}
Basically, I create a VoiceChangerFunction function (object) that takes a Voice & as first parameter and returns void.
Later, I bind a function that will take as first parameter the one I'll give to it when I call it, and another parameter that I give when I bind it (100, in my example).
Right?
What I don't understand is: then, this function is passed to UpdateVoices(), which take as input a function/object that has 1 param (Voice &), not 2 as created in my bind function (Voice &, unsigned int).
How can it works?
Its like to have void VoiceChangerFunction(Voice &voice) and call VoiceChangerFunction(Voice &voice, unsigned int value ).
The function prototype is different. I mean: the callback bind I created isn't a VoiceChangerFunctions function, because it takes more parameters.
How can it works/match?
That is exactly the beauty of bind and std::function at works. You are defining the callback as function taking one argument, and bind is returning a function object which takes one argument.
The main point here is that it actually calls the function which takes 2 parameters, but the second one is fixed, and will always be 100 (in your case). This is the sole purpose of binders - to provide a way to call functions with different set of arguments with some fixed values. If you would be calling the function taking the same set of arguments, there would be no reason to use binders at all!
Knowing that bind is similar to lambdas, the same code could be written as - and probably be more clear:
VoiceManager::VoiceChangerFunction callback;
callback = [](Voice& v) { VoiceManager::SetValue(v, 100); };
voiceManager.UpdateVoices(callback);
And if you are curious how it works, you might try to create a binder framework yourself. If you are only doing it for educational purposes and not worried about too many details, it is not that hard.
When you bind, you're making a new function that only takes Voice as a param, that's why it works.
void a_func(int x) { return; }
std::function<void(void)> new_func = std::bind(&a_func, 1);
new_func now has the signature of void(void), so you could pass it around to anywhere that expects a function of type void(void).
When you call new_func, it really calls a_func(1).
Your assumption about bind is wrong.
Your bind call returns a function object that will accept one parameter, namely the placeholder. The other parameter on your function is already bound to 100.
A little example:
void foo(int i1, int i2) {};
std::function<void(int,int)> fn1 = std::bind(foo, std::placeholders::_1, std::placeholders::_2);
std::function<void(int)> fn1 = std::bind(foo, std::placeholders::_1, 1);
std::function<void()> fn1 = std::bind(foo, 1, 1);
The bind will create a matching function depending on bound and unbound parameters.
Update
The compiler will generate a struct from the bind expression and a copy of your parameter. Simplified something like this(this will not compile):
struct Function_{
void(*fn)(Voice &, unsigned int)
unsigned int i_;
Function_(void(*f)(Voice &, unsigned int), unsigned int i):fn(f),i_(i){}
void operator()(Voice& v){
fn(v, i_);
}
}
fn is the first parameter which is a function pointer and the bound (100) is the second. Then all you need is some type erasure and your own bind is ready to go.
I am porting assembly code to C++ and I have some problems porting a portion of it.
I have a pointer declared as:
void* function;
and it is pointing to a function call:
void* function = getfunction(lib, fname);
I have a inline assembly code which I have to migrate to C++:
__asm { call function }
How to call this pointer in C++?
Please note that the arguments lib and fname are of type void*.
And void* function points to value returned by getfunction().
In what follows, I am assuming that the entire function call is encapsulated by
__asm { call function }
In which case this is a function pointer that can be declared like this:
void (*function)(void);
The __asm block does not push parameters onto the stack. Therefore the parameter list is empty. And the __asm block does not extract a return value. So the function has void return type.
You assign to the function pointer in just the same way:
function = getfunction(lib, fname);
And you call it like this:
function();
Look at the following example :
// ptrFunc is the name of the pointer to the getFunction
void* (*ptrFunc)(void*, void*) = &getfunction;
// ... Declaring whatever lib and fname are
// Now call the actual function by using a pointer to it
ptrFunc(lib, fname);
// Another form of how to call getFunc might be :
(*ptrFunc)(lib, fname);
Also, you can pass function pointers to another function such as :
void *getFunction(void* lib, void* fname)
{
// Whatever
}
void myFunction( void* (*ptrFunc)(void*, void*) )
{
// void *lib = something;
// void *fname = something else;
ptrFunc(lib, fname);
}
int main()
{
void* (*ptrFunc)(void*, void*) = &getfunction;
// Passing the actual function pointer to another function
myFunction(ptrfunc);
}
I have the following code:
#include<iostream>
using namespace std;
typedef void (*HandlerFunc)(int, int);
HandlerFunc mr;
HandlerFunc mm()
{
return mr;
}
void sample(int a, int b, HandlerFunc func)
{
}
void main()
{
sample(1, 2, mm);
}
Here I'm trying to pass a function of type HandlerFunc to another function, but I am getting an error:
Error :*: cannot convert parameter 3 from 'void (__cdecl *(void))(int,int)' to 'void (__cdecl *)(int,int)'
If I type cast as sample(1, 2, (HandlerFunc)mm); everything works fine.
Can anyone tell what is the way to solve the error issue?
HandlerFunc mm()
{...}
should be:
void mm(int, int)
{...}
Your function sample() takes a pointer to function(you typedefd as HandlerFunc) as last argument.
The address of the function you pass as this argument must match the type of the function to which it is a pointer.
No, no, you've confused the types. mm is a function that returns a function pointer of the appropriate type. However, mm itself is not of the appropriate type - it doesn't accept any parameters.
You should pass mr in main, or pass mm() (that is - call mm and pass the return value)
The code in main should be:
sample(1,2,mm());
as mm returns a HandlerFunc, it isn't one itself.
HandlerFunc mm()
{
return mr;
}
This means nm is function which will not receive any agruments(void), and it will reutrn a pointer to function of type void (*)(int, int)
so that only you are getting that error.
I would like to forward a callback to a function pointer. So I declare a static (int*) m_pointer1, as well as a method void RegisterCallback1( (int*)fct)
in class1.h:
public:
int RegisterCallback1( int (*fct) );
private:
static int (*m_Callback1);
in class1.cpp:
int class1::RegisterCallback1( int (*fct) )
{
m_Callback1= fct;
}
then, I want to forward the callback to the function pointer:
void class1::Callback1()
{
(*m_Callback1)();
}
But I get a compiler error "Expression must have (pointer-to)- function type
I have followed tutorial and read about function pointers and they seem to do it this way without any problems. Any ideas why?
EDIT:
So, I declare (int*)(void)m_Callback1 -Visual Studio requires a void there...-
Then how do I call the registerCallback function with the argument?
class1.RegisterCallBack1( ??? - class2::callback -??? );
static int (*m_Callback1) does not declate a function pointer, just a pointer to int: you forgot about the parameter list. You meant:
static int (*m_Callback1)();
and
int RegisterCallback1( int (*fct)() );
You haven't declared a function pointer, you've declared a normal data pointer. You are missing () at the end of the declaration.
You can try to limit the missing () errors pointed out by Oli and Dave by using a typedef for the callback function's signature: typedef int (*)() CallBack; This would at least have the merit of letting you think once about the precise number of brackets rather than at every point in your code where you use such a function.
how do I cast void *something to an object in standard C++?
Specifically I want want to cast void *userdata
to std::map<String, void*>
Is this possible? I am trying:
//void *user_data is a parameter of this function (callback)
std::map <String, void*> user_data_n; //this line is ok
user_data_n = static_cast<std::map<String, void *>>(*user_data); //I get the errors here.
ERRORs:
Spurious '>>' user '>' to terminate a template argument list
Expected '>' before '(' token
'void *' is not a pointer-to-object type
or is there a better way to carry information about the caller object and some other parameters I can pass to void *user_data?
UPDATE:
Ass suggested by #aaa carp I changed >> to > > and the first two errors were solved. The last is strange, Why do I get that kind of message when casting it here and not when putting that object when setting the callback?
std::map<String, void*> user_data_h;
user_data_h["Object"] = this; //this is a MainController object
user_data_h["h"] = h; //h was defined as int *h
createTrackbar("trackbar_H", winName, h, 255, trackbar_handler, &user_data_h);
where createTrackbar is defined as:
int createTrackbar( const string& trackbarname, const string& winname,
int* value, int count, TrackbarCallback onChange, void* userdata);
UPDATE2:
doing this solved my problem but following the same approach, why I still get error when trying to cast objects contained in my map object?
void trackbar_handler(int value, void *user_data){
std::map <String, void*> *user_data_map;
user_data_map = reinterpret_cast<std::map<String, void *> *>(user_data); //WORKED!! ;)
MainController *controller; //the same class type I put using "this" above
controller = reinterpret_cast<MainController *>( user_data_map["Object"]); //ERROR here
int *var = reinterpret_cast<int*> (user_data_map["h"]); //ERROR also here
>> should be > >
and you do not want to dereference void pointer, instead cast void pointer to desired pointer type and then dereference
#casa has already provided you with answer to second problem
When you're casting from a void *, your result will be a pointer too. So the map declaration should be:
std::map <String, void*> *user_data_n;
Second, you should use reinterpret_cast for such (potentially dangerous) casts:
user_data_n = reinterpret_cast<std::map<String, void *> *>(user_data);
Update:
As others suggested, you could simply use a static_cast as well.
Why do I get that kind of message when casting it here and not when putting that object when setting the callback?
Any pointer can be implicitly converted to void *, but when converting it back to a pointer of some specific type, you need an explicit cast.
why I still get error when trying to cast objects contained in my map object?
As already mentioned in the comments, you need to dereference the pointer before using the map object. You might want to define a reference instead to make things easier:
std::map <String, void*> &user_data_map =
*(static_cast<std::map<String, void *> *>(user_data));
An noted, the >> in that line to close your template should be > > (with a space).
Also, if user_data is a void pointer, you cannot dereference it. You could cast the pointer to another pointer type with reinterpret_cast:
std::map <String, void*> *user_data_n_ptr; //note this is a pointer to a map.
user_data_n_ptr = reinterpret_cast<std::map<String, void *> *>(user_data);
This will cast the void pointer to a std::map .
You should be careful with this. void pointers shouldn't typically be thrown around in c++. There may be a better way to do what you want and avoid void * all together.
I suppose this is for serving a C callback? It might be better to have a specialized struct which keeps all those values using the exact types. That way you'd be down to one cast for the whole thing. Something like this:
struct callback_user_data {
my_class* that;
int number;
callback_user_data(my_class* p, int i) : that(p), number(i) {}
};
// the callback
void my_callback(void* user_data)
{
callback_user_data* cbud = static_cast<callback_user_data*>(user_data);
somehow_use(cbud->that, cbud->number);
}
//call the function, passing our user data
callback_user_data cbud(this, 42);
some_function_taking_our_callback(&my_callback, &cbud);
Note that usually I have this seen (and used) this so that not a special type is passed, but only this, which has all the necessary data anyway:
// the callback
void my_callback(void* user_data)
{
my_class* that = static_cast<my_class*>(user_data);
that->f();
std::cout << that->number << '\n';
}
//call the function, passing our user data
some_function_taking_our_callback(&my_callback, this);