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.
Glorfindel
  • 571
  • 1
  • 7
  • 18
Adam Ryczkowski
  • 259
  • 1
  • 4
  • 14
  • 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 '18 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? – Adam Ryczkowski Dec 16 '18 at 07: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 '18 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". – AnT stands with Russia May 21 '19 at 17:24
  • I mean the event driven execution, i.e. cooperative multitasking. I suppose you are not aware if anyone wrote such library? – Adam Ryczkowski May 21 '19 at 21:58
  • This is not on the whole an unreasonable thing to want to do. I've just been asked to port a framework I wrote on the ESP8266 over to vanilla Arduino and guess what - I've made extensive use of std::function for callback registration because of the elegance and modularity it enables. In a thin-and-light environment this is an extremely useful thing to be able to do. – Phil Haigh Mar 20 '20 at 09:18

1 Answers1

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.

Adam Ryczkowski
  • 259
  • 1
  • 4
  • 14
  • I've changed the address of the repository - by mistake I pointed you to an old and defunct one. The proper address is https://github.com/fopeczek/function_objects – Adam Ryczkowski Mar 31 '21 at 07:56