in C/C++ (possibly pre-C++11), is it possible to do
A (*eval(A (*function)(B), B b))(){
// ... ??
}
i.e., a function taking
- a function returning an A value from a B value,
- a B value to be fed to that function,
which returns - a function returning an A from () ...??
If yes, would it be
- efficient??
- guaranteed the compiler generates code which is not executed before call of the returned function??
Thanks in advance & cheers, Nick
2014-4-20 (1): Thanks for mentioning the 'evtl.'(fixed) std::bind. :-)
So – to understand – (in C/pre C++11 without Boost) function pointers are exceptional in the way that, inside functions, it is only possible to declare them, but there is no way to produce or modify an instance – as function/method definitions are the only possible sources for function pointer instances, from where these may be handed over either explicitly, or by function/method arguments??
Just asking, as I am not clear about a possible internal representation of function pointers...
2014-4-20 (2): With the contribution of Danvil, it's time for the purpose to reveal, here the same with templates:
template<typename T,typename A>
struct Evaluator {
T(*f)(A);
A a;
T operator()() const { return f(a); }
};
template<typename T,typename A>
Evaluator<T,A> eval(T(*f)(A), A a) {
Evaluator<T,A> w;
w.f= f; w.a= a;
return w;
}
This works, while – as some already might guess – the whole, from arbitrary matching function/arguments collections, is intended to be sent as a zero parameter procedure into a single function/method handling execution similar to a try/catch.
For not having to use mostly identical code for each different parameter count, the actual idea was to generate the still not executed job as a such zero parameter procedure of same type for all cases.
Still, I do not find a way how to construct or modify a function pointer inside a function; 'typecasting' in some way to Evaluator does not seem practicable, does it??
Again, thanks a lot, and Happy Easter... :-)