hirotakaster / CoAP-simple-library

Other
111 stars 48 forks source link

Coap-simple-library no longer usable with C++ objects #22

Closed jackjansen closed 3 years ago

jackjansen commented 3 years ago

The fix for #20 was (apparently) to revert the changes for #18.

18 added use of std::function for the callback function (as opposed to C-style function pointers). This enabled use of CoAP-simple-library in C++ programs where you have your callback methods inside objects.

Apparently this broke for @al3xsh hence the std::function was reverted for #20.

So, now it is broken for my use case again:-)

A solution that works for both use cases may be to use an ifdef around the include/typedef, so that it works both for older compilers (using a function pointer) and newer ones (using a std::function). Maybe something like (untested):

#if __cplusplus >= 200103L
#include <functional>
std::function<void (CoapPacket &, IPAddress, int)>CoapCallback;
#else
typedef void (*CoapCallback)(CoapPacket &, IPAddress, int);
#endif
hirotakaster commented 3 years ago

@jackjansen oh...I forget this problem. uhmmm, okay I will clear this C/C++ STL issue.

hirotakaster commented 3 years ago

@jackjansen cpp predefined macro is ... Arduino IDE : avr-g++ -dM -E -x c++ /dev/null

define __cplusplus 199711L

esp8266 xtensa-lx106-elf-g++ -dM -E -x c++ /dev/null

define __cplusplus 199711L

esp32 xtensa-esp32-elf-gcc -dM -E -x c++ /dev/null

define __cplusplus 199711L

so I think it's better that separate with ESP8266/ESP32 ifdef flags.

jackjansen commented 3 years ago

Bah. Apparently the xtensa compilers are not 100% c++11 compatible, if I read the standard correctly you can only set cplusplus to whatever you're fully compatible with. `has_include()` may work, but it's a c++17 feature so not sure if it is supported on older platforms...