The blog post Automatic Memoization in c++0x provides an function for producing a memoized version of an existing function. The blog post and the associated code have been discussed previously on stackoverflow (e.g. What does this C++11 code do?), however, none of these solutions is able to provide a fully universal memoizer which is able to correctly memoize recursive functions as well.
Sure, there is the trick of changing the recursive call by using something like this (assuming we have a memoizer such as the one presented in the blog post called memoize
already in place):
std::function<int (int)> f;
int fib(int n) {
if (n < 2) return n;
return f(n-1) + f(n-2);
}
int main(void) {
f = memoize(std::function<int (int)>(fib));
}
But this feels more like a workaround than a proper solution because we still need access to the function which we want to memoize. A proper solution should be able to fully memoize any function including the ones which are defined in some library. However, producing such a solution, so it seems, is beyond my reach (assuming it is possible), therefore I'm asking:
Is a truly universal memoise function possible?
How can one achieve such a feat?
And if this is not possible, is there at least a way to generalise the above approach. Something along the lines of (does not compile and is not valid C++):
int fib(int n){
if (n < 2) return n;
return this_func(n-1) + this_func(n-2);
}
Where this_func
is something which is similar to the this
pointer of a class but for a function. [Edit: This would probably still suffer from the problem that the this_func
pointer would point to fib
instead of the memoized fib
]
As the cache needs to be shared across function calls, you'd either have to pass it as an argument, or share it otherwise. One way to share it is using a function object:
Where you can factor out the
memoize
part.There's also a trick with temporary lifetime to pass the memoized function as an argument; something like this:
Yet, this requires a change of
memoize
, as therecurse const&
cannot (and shouldn't) be part of the internalmap
.N.B. those
const&
could also be&&
for lifetime extension, yet, that might be confusing due to move semanticsFollowing may help, but it is still a hack, and it is ugly... :
I use
void*
since the correct signature is recursive :-/My guess is that this is not possible while staying within the bounds of defined behavior, because there's no way to abstract out the recursive call. In fact, I can't think of anything significantly better than the global variable version that you provided.
One obvious idea to provide an abstraction point more robust than a global variable would be to add the function to recurse on as a first argument:
Then, you could modify your memoize function to pass the memoized version into the first argument, and things should just work. This trick is often used to accomplish the same goal in (unityped/untyped) functional languages like scheme.
However, this sort of trick relies on something called the "Y combinator" which I think can't exist in C++ as it has an infinite type.