xieite ::memoize ()
Defined in header <xieite/fn/memoize.hpp>
Memoizes a function call to immediately return its result when called with the same arguments again. This can be used to avoid repeating expensive calculations.
Declaration
template <typename F ,typename... Args >requires (std ::invocable <F ,Args ...>)std ::invoke_result_t <F ,Args ...>memoize (F fn ,const Args &... args )noexcept (std ::is_nothrow_invocable_v <F ,Args ...>);
Example
Output:int add (int x ,int y ) {std ::"`add( ,{} ,{} )` called\n "x ,y );return x +y ; }int main () {std ::" ,{}\n "xieite ::memoize (add ,1 ,2 ));std ::" ,{}\n "xieite ::memoize (add ,1 ,2 ));// `add(1, 2)` is not called again }
[View in Compiler Explorer]`add(1, 2)` called 3 3