I've got a C library that takes a function pointer to register commands. I want to use this in my C++ application. I've tried to use std::function in combination with std::bind to create a C compatible function pointer that will call my member function inside a class. When trying to pass the std::function, I get an compilation error.
// The way the C library typedef's the function
typedef int (*console_cmd_func_t)(int argc, char **argv);
// Function to register the callback needs a struct
struct {
console_cmd_func_t func;
} console_cmd_t;
void console_cmd_register(const console_cmd_t *cmd) {
// Register the command
}
// In my C++ class
typedef std::function<int(int argc, char **argv)> ConsoleFunction;
ConsoleFunction fn = std::bind(&MyClass::consoleCommandHandler, this, std::placeholders::_1, std::placeholders::_2);
const esp_console_cmd_t runCommand = {
.func = fn
};
console_cmd_register(&runCommand);
However, this results in the following error:
cannot convert 'ConsoleFunction' {aka 'std::function<int(int, char**)>'} to 'console_cmd_func_t' {aka 'int (*)(int, char**)'} in initialization
Obviously its not the same definition. If I try to correct that however:
typedef std::function<console_cmd_func_t> ConsoleFunction;
I get the following error:
variable 'ConsoleFunction fn' has initializer but incomplete type
How can I successfully register the command?