このコードを含む記事を見つけました:
template <typename ReturnType, typename... Args>
std::function<ReturnType (Args...)>
memoize(std::function<ReturnType (Args...)> func)
{
std::map<std::tuple<Args...>, ReturnType> cache;
return ([=](Args... args) mutable {
std::tuple<Args...> t(args...);
if (cache.find(t) == cache.end())
cache[t] = func(args...);
return cache[t];
});
}
これを説明してもらえますか?ここでは多くのことを理解できませんが、最も奇妙なことは、キャッシュがローカルで静的ではないことですが、おそらく私は間違っていて...