Defined in header <memoized_invoke.hh>

void reset() noexcept;                               // (1)

void reset( std::decay_t< Args >... args )           // (2)
    requires ( sizeof...( Args ) > 0 );

1) reset()

Clears the cached result and, if the state was done, transitions it back to not-started. The stored arguments are kept. The next operator() re-runs the callable with them.

noexcept. A no-op if the object was never called.

2) reset(args...)

Clears the cached result, replaces the stored argument set with args..., and returns the state to not-started. Available only when sizeof...(Args) > 0. Nothing runs until the next operator().

Unlike operator()(args...), reset(args...) does not compare the new arguments with the old — it always replaces and always clears — and it does not invoke the callable. Use it to stage a new argument set now and defer the work.

Parameters

ParameterDescription
args(overload 2) The argument set the next call will use. Taken by value as std::decay_t<Args>....

Return value

None.

Exceptions

Overload 1 is noexcept. Overload 2 can throw only if constructing the argument tuple from args... throws (e.g. a throwing move of a stored argument type).

Notes

Neither overload is thread-safe, even under lock_free: both mutate the cached-value optional (and overload 2 the argument tuple), which are non-atomic members. Reset a shared lock_free object only with every other thread quiesced.

Typical uses:

  • A SIGHUP handler calling reset() on a lazily-loaded config wrapper to force a reload.
  • A test fixture calling reset() between cases.
  • reset(new_args...) to re-target a wrapper without paying for the recomputation until the value is actually needed.

Example

#include <memoized_invoke.hh>
#include <cassert>

using fedem::utility::memoized_invoke;

int main()
{
    int runs = 0;
    memoized_invoke co( [&runs](int x){ ++runs; return x + 1; }, 10 );

    assert( co()  == 11 );  assert( runs == 1 );
    assert( co()  == 11 );  assert( runs == 1 );   // cached

    co.reset();
    assert( co()  == 11 );  assert( runs == 2 );   // re-ran, same arg

    co.reset( 41 );
    assert( !co.is_done() );
    assert( co()  == 42 );  assert( runs == 3 );   // re-ran, new arg
}

See also