I want to assign a function's address to a function pointer, but the function to be addressed returns a function pointer with the same signature as itself, causing it to recurse in a way that I can't write the return type at all, for the function pointer or even the function declaration itself...
I guess a way of simplifying the problem so it's not confusing:
How could I write a function declaration such, that it can return a pointer to itself (or any other function with the same signature)?
????? function(int a){
// could be this function, or another with the same signature,
return arbitraryFunction;
}
?????(*func)(int) = function; // same problem as above
edit:
For now I have a solution, though I won't post it as an answer because it's aggressively ugly. It gets rid of the recursion by simply returning a raw void* pointer as the return type, and ends up taking the following form:
void* function(int parameter){
return arbitraryFunction; // of the same signature
}
void*(*func)(int) = function;
func = reinterpret_cast<void*(*)(int)>(func(42)); // sin
edit2:
It seems casting between function pointers and regular pointers is UB, so I can't use void* in this case...
To answer one of the comments, this is for passing control between multiple "main" loops in my program, with each loop getting it's own function. There's a lot of ways to do this, but returning function pointers (or NULL to terminate the program) mid-loop seemed like the simplest method, but I didn't anticipate that pointers to data and pointers to function addresses would be incompatable with each other. I think returning polymorphic function objects will end up being the more sane option in this case.
void*as the function's and function pointer's return type, will let me avoid the recursive declaration...ocamlcwith-rectypes(for recursive types)struct. This is an equivalent of isorecursive types as now compiler may determine pack/unpack borders by the fact you've created/accessed your structure.