I need to pass order of a hundred callback functions to a C library. All my callbacks will be very similar, except that they need to know which "key" it was registered for. The C API doesn't allow an context to be passed back.
In other words this is the API:
void register_for_event(int event_type, void(*callback)());
I came up with this template metaprogramming:
template<int event_type>
void event_handler() {
std::cout << "Event: " << event_type << std::endl;
}
template <int n = 100>
void register_event_handlers() {
register_for_event(n, event_handler<n>);
register_event_handlers<n-1>();
}
template<> void register_event_handlers<0>(){}
But is there a way to write this:
- easier to read
- faster to compile (the above adds several seconds to building the file, with gcc)
Any version of C++, up to C++20, would work for me.
I'm trying to avoid macros, though it's not obvious to me how macros would even help here.
I could use code generation from shell script. I may end up doing this if I can't find a better C++-native way. But I'm also interested in what other native C++ ways are possible.