I'm writing a class to wrap around a library that requires callback function pointers. See below:
struct LibraryConfig {
// Omitting other members...
void (*callback)(const char *);
};
class MyClass {
private:
LibraryConfig m_config;
public:
MyClass(const LibraryConfig &config) {
// Initialize m_config using config, would like to set callback so that it calls
// this->myCallback().
}
void myCallback(const char *);
};
Only static instances of MyClass will be declared, so construction can be kept within compile-time. I've tried lambdas and template functions that take MyClass pointers, but I either can't accomplish this within the constructor, or can't achieve this at compile-time (getting the address of an instance through this
or &myClass
at compile-time doesn't seem possible).
constexpr
parameters may be allowed in the future, making this trivial to implement, but is there a way to accomplish this right now with C++20?