I've been banging my head against the wall for a good long while on this one...
1 2 3 4 5 6 7 8 9 10 11
void dummy(void* (*f)(void*))
{
// Use f to start some threads, start a timer, launch the nukes, etc
}
std::function<void*(void*)> func;
auto magic = // insert magic here to convert func to (void* (*f)(void*);
dummy(magic);
Any help would be appreciated...I can't believe that Google has failed me this time.
void foo( std::function< void* (void*) > func )
{
if( func ) // if the call-wrapper has wrapped a callable object
{
typedefvoid* function_t( void* ) ;
function_t* ptr_fun = func.target<function_t>() ;
if( ptr_fun != nullptr )
{
// the call-wrapper has wrapped a free function
// and ptr_fun is a pointer to that function
// use ptr_fun
}
else
{
// the callable object wrapped by the call-wrapper is not a free function of this type
// it is a closure, a function object, a bind expression, or a free function with a
// different (though compatible) type - for example: int* function_type( const void* ) ;
}
}
}
Excellent, thank you. If you don't mind my asking, where did you find the information about std::function to make that example from?
* Also, is there a way to convert a closure into a raw function pointer?
Normally, I would simply use boost::thread for what I'm trying to do, but I cannot in this case. What I'm trying to do is make a common, simple wrapper interface for encapsulating posix and win32 threads. I would have hoped to just use std::function to accomplish this...but as expected, it's fighting me as hard as it can.
> * Also, is there a way to convert a closure into a raw function pointer?
If there is no lambda-capture, a closure can be implicitly converted to a pointer to function with the same parameter and return types. This was a late addition to the IS, so you may need to check if the compiler is conforming.
1 2 3 4 5 6 7
int bar( int (*pfn)( char ) ) { return pfn('A') ; }
int main()
{
auto closure = [] ( char c ) { return +c ; } ;
std::cout << bar( closure ) << '\n' ;
}
Alright...I'll need to read up on GCC's documentation (again) to see if there are any unresolved issues with this, as I'm getting odd results. It seems like I'm doing more research to form workarounds than actually getting the project done. How I miss only targeting Windows systems..