Extend usage of std::function on all the platforms where it's available#993
Extend usage of std::function on all the platforms where it's available#993alranel wants to merge 1 commit intoknolleary:masterfrom
Conversation
|
Hi, faced the issue with the same reason, on Arduino Nano Connect, based on RP2040. |
|
+1 |
|
Hi everyone, I’m using PubSubClient version 2.8 on an Arduino Opta WiFi board (not ESP32/ESP8266). I want to set a callback using a lambda with capture ([this]), like this:
However, I get the following compilation error: error: no matching function for call to 'PubSubClient::setCallback(OptaMQTT::begin()::<lambda(char*, uint8_t*, unsigned int)>)' My compiler uses the flag -std=gnu++14, so lambdas with captures should be supported. Looking into the library header, I found this conditional macro:
So it seems that only on ESP8266/ESP32 the library uses std::function for callbacks (allowing lambdas with captures), and on other platforms like Arduino Opta WiFi it falls back to plain function pointers. My questions are:
Thanks for any insights! |
|
@lucae98 Can you please try https://github.com/hmueller01/pubsubclient3 on this. If your platform supports |
|
@hmueller01 Thanks for your fast response! |
For your information: I got it working now. Tested on the Opta WiFi using the Mosquitto broker.
No issues with version 3.1.0. |
|
Hint: On PubSubClient3 you should use |
At this point in time, C++11 is available on most Arduino platforms and not just on ESP8266 and ESP32.
With this pull request, the ability to supply lambdas (including object methods) to
setCallback()instead of plain function pointers is extended automatically to all the platforms which provide support for std::function. :)