I need to wrap a callback mechanism in a C interface in C++ accepting callables. In the C interface there is a method to set the callback:
void c_set_callback(void(*callback)(void* context), void* context);
Here is my attempt to wrap this with a callable:
template<typename Function>
void set_callback(Function&& function)
{
c_set_callback
([](void* context)
{
(*reinterpret_cast<typename std::decay<Function>::type*>(context))();
}
, reinterpret_cast<void*>(&function)
);
}
But I can't figure out the right way of casting the context for the universal reference to "function". With the implementation above I get a segmentation fault when the callable is invoked from within the lambda.
Please see https://onlinegdb.com/r1WeuBz08 for the full code example.
Aucun commentaire:
Enregistrer un commentaire