template <typename R, typename... Args> auto memoize(R(*fn)(Args...)) { std::map<std::tuple<Args...>, R> table; return [fn, table](Args... args) mutable -> R { auto argt = std::make_tuple(args...); auto memoized = table.find(argt); if(memoized == table.end()) { auto result = fn(args...); table[argt] = result; return result; } else { return memoized->second; } }; }
g++ -fno-elide-constructors
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.