Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting function pointer to functor in C++

Tags:

c++

c++11

I have a function pointer type imported from another .hpp file. Something like:

typedef void (*PFN_func)(int i);

I want to create a functor of the same type:

std::function<PFN_func>

But this doesn't work. I don't want a solution like

std::function<void(int)>

Because mt function pointer definition is much more complicated

like image 417
user972014 Avatar asked Sep 16 '26 08:09

user972014


1 Answers

You could do this:

std::function<std::remove_pointer<PFN_func>::type>

Removing the pointer from void (*)(int) gives the function type void(int).

For the case of a general callable, see Is it possible to figure out the parameter type and return type of a lambda?

like image 142
Brian Bi Avatar answered Sep 19 '26 01:09

Brian Bi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!