Search code examples
c++raspberry-pic++17std-function

Convert std::function<void ()> to void (*)()


So I have a member function in C++ that takes in an std::function<void> and I want to pass this into a wiringPi function:

void my_class::on_change(std::function<void()> func) const
{
    wiringPiISR(
        wiring_pi_pin_number_,
        INT_EDGE_BOTH,
        func);
}

I am getting the error cannot convert ‘std::function<void()>’ to ‘void (*)()’ in initialization. I have looked online and I am only finding things that have a hundred lines of pointer and reference casting and do not compile on a pi. Looking for any help here. Should I be using void (*)() all the way up the stack? This seems like the wrong approch. I can find plenty about converting a void (*)() to a std::function<void> but not the other way around..

PS: I am a noob at c++. My main language is C# and some other higher languages so still learning this one and not sure what I am missing here..


Solution

  • Convert std::function<void ()> to void (*)()

    You cannot.

    Should I be using void (*)() all the way up the stack?

    Yes.

    Technically using std::function could work but you'd have to rely on global state and the benefits of using std::function may not be worth that cost.

    This seems like the wrong approch.

    It's not, given the limitations of the API that you use.