5

Both ArduinoSTL and StandardCplusplus don't support them. They implement the version that is compatible with c++03 (not c++11).

This functionality is handy when you design libraries that work with callbacks. And you do need them, if your Arduino does more than one thing simultaneously.

I tried also several other options, but none was enough. Most complete thing I found, Passing capturing lambda as function pointer, does not support plain function pointers and does not allow me to hold the function object as a member of the structure, because the type of the object depends on the lambda (not only its signature, like in std::function).

In particular, I need a functor that

  • can store both capturing lambdas and plain function wrappers
  • has a type that depends only on the signature of the function, so I can store it as a class member.
6
  • Well, ARM compilers with C++11 support usually have working functional library and other STL stuff. For the AVR's you can try to implement something own, or copy something like in mbed (its online compiler doesn't support c++11 so they have some not very good wrapper classes for callbacks)
    – KIIV
    Dec 15, 2018 at 18:44
  • @KIIV Writing a proper substitution of std::function seems very intimidating to me. Do you know something that is already written, that I can use to store both capturing lambdas and function pointers? Dec 16, 2018 at 7:34
  • It can do just function pointers and as a bonus even method pointers, but it's not for the c++11 so no lambdas.
    – KIIV
    Dec 16, 2018 at 11:41
  • Storing capturing lambdas and function pointers will require implementing some kind of type-erasure. And that is exactly what std::function is. You will have to implement some version of std::function. You can make a simplified implementation, but there's no way around it. Although it is not clear why you claim that you need callbacks to do "more than one thing simultaneously". May 21, 2019 at 17:24
  • I mean the event driven execution, i.e. cooperative multitasking. I suppose you are not aware if anyone wrote such library? May 21, 2019 at 21:58

1 Answer 1

1

We have written a solution, that we published as a Platformio library: https://github.com/fopeczek/function_objects.

Please note, that each functional object does one allocation, and those are not particularly friendly in an embedded environment. I am not even sure, that the Arduino C++ does heap deallocation, so you may be leaking resources if you create and destroy the functional objects in a loop.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.