I'm trying to register a function that returns an int
to be called at the end of a program using the atexit()
function. (Specifically, the endwin()
function from ncurses.)
But since atexit()
needs a pointer to a void
function, I ran into a problem. I tried the following:
static_cast<void (*)()>(endwin)
but static_cast
ing from an int
function to a void
function doesn't seem to be allowed.
Is what I'm trying to accomplish possible at all, and if yes, how?
Note: I'm willing to just ignore the return value of the function.
Edit: I also tried creating a lambda function, which seems to do what I want:
atexit([]{ endwin(); });
Is this a good solution compared to a wrapper/forwarding function? (Other than that it needs C++11 and avoids defining a new function whose sole purpose is just forwarding another function.)
Function pointers can't be converted. Just register a forwarding function:
#ifdef __cplusplus
extern "C"
#endif
void endwin_wrapper() { endwin(); }
...
atexit(endwin_wrapper);
Since you tagged your question C++: if you define the forwarding function in C++ you need to declare it to be extern "C"
to have the proper language linkage.