samedi 20 novembre 2021

Is there a useful "reference" that can be acquired from a std::function back to the stored function/lambda

In C if one wants to know/acquire a useful reference back to a callback/some other function, it can be done quite easily be casting the function to a void*. Later, in debug for example, the pointer be examined and traced back to the original function (again as an example, via the compiler map output or even the in editor debugger).

This sort of information is very useful when using "breadcrumbs" - eg. a circular buffer of void*s - to debug the flow of an application.

In C++ with std::function, it is possible to get a raw pointer via the target<func_type>() member function, however this only works if you know ahead of time the precise type of the stored target (i.e. a standard C function), however when storing lambdas this is no longer the case as the target is not longer a simple void() for a std::function<void()> and will therefore return nullptr.

The above being the case, what is the next best reference once can get to the stored target such that it can be used, preferably, after the execution of the application has finished or while execution is paused with the debugger attached?

Alternatively can the target be acquired via some template magic, while still retaining the semantics/usability of std::function within the library code? This would need to include capturing lambdas and pure C functions

A few notes:

  • I am not asking how to do debugging
  • I am not asking how debug information could be captured from within the stored target - imagine this is a library rather than client code

Aucun commentaire:

Enregistrer un commentaire