I have a simple class (details here)

namespace MemoizerSupport {
    template <typename T1, typename T2>
    using DEFAULT_CACHE = LRUCache<T1, T2>;

template <typename RESULT, template <typename, typename> class CACHE = MemoizerSupport::DEFAULT_CACHE, typename... ARGS>
struct Memoizer {
    Memoizer (const function<RESULT (ARGS...)>& f, CACHE<tuple<ARGS...>, RESULT>&& cache = CACHE<tuple<ARGS...>, RESULT>{});

    nonvirtual RESULT Compute (ARGS... args);

    function<RESULT (ARGS...)>    fFunction_;
    CACHE<tuple<ARGS...>, RESULT> fCache_;

usable as follows:

unsigned int totalCallsCount{};
Memoizer<int, MemoizerSupport::DEFAULT_CACHE, int, int> memoizer{
    [&totalCallsCount](int a, int b) { totalCallsCount++;  return a + b; }
VerifyTestResult (memoizer.Compute (1, 1) == 2 and totalCallsCount == 1);
VerifyTestResult (memoizer.Compute (1, 1) == 2 and totalCallsCount == 1);

My problem is that I want to be able to have the 'default cache' object DEFAULT, and to deduce the other parameters:

// doesn't work
Memoizer  memoizer{[&totalCallsCount](int a, int b) { totalCallsCount++;  return a + b; }};

I know I can replace the argument definition for the Memoizer template with a 'typename FUNCTION' to allow about anything to match, but then I don't get the deduced types which I need for the cache.

And I'd like to be able to put DEFAULT_CACHE argument at the end of the typename name (in the Memoizer template), but then I cannot have the template be variadic in the number of arguments to the memoizer?

Is this something C++ templates just don't support yet, or is there some hint/pattern someone can offer to make this work a little more smoothly??

  • 1
    Hi Lewis, this is an interesting question. But it's really more about programming than engineering and therefore should belong to StackOverflow – Christophe Aug 17 '18 at 17:00
  • @Christophec thx. I'll try there. I'm still a little fuzzy on the distinction – Lewis Pringle Aug 18 '18 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.