title: Static Variable in Template Function Doesn't Seem Unique to Template Instance
body: <p>I'm trying to learn about <a href="http://en.wikipedia.org/wiki/Memoization" rel="nofollow">memoization</a> using C++ along with boost, and the C++11 spec.  However I've run into a problem that I'm having trouble wrapping my head around.  I'm following a tutorial here: <a href="http://programminggenin.blogspot.com/2013/01/memoization-in-c.html" rel="nofollow">Memoization in C</a> and the tutorial says that you can generalize the memoization of recursive functions using a templates and lambda functions.  The tutorial also lists recursive factorial and fibonacci functions to use with the template.  However, the guide only uses the fibonacci function.  </p>
  
  <p>I wrote a test program to see how this all worked and make both a memoized fibonacci AND factorial function in the same run.  Thing is, the memoization template uses a static map to store cached values and it seems like the map isn't unique to each instance of the memoized functions.  Is this expected?  What should I do to make the map unique to each template instance?  Before I started using C++11 features, I had tried to create a template class that accepted a boost function to encapsulate this process.  Would the static map be unique in a template class instead?</p>
  
  <p><strong>Main Logic for Creating Memoized Functions</strong></p>
  
  <pre><code>// Template function to create memoized versions of recursive lambda functions
  template&lt;typename inType, typename outType&gt;
  std::function&lt;outType(inType) &gt; memoize(std::function&lt;outType(inType) &gt; foo) {
     return [foo](inType n) {
        static std::map&lt;inType, outType&gt; memo;
  
        outType ret;
        if (memo.count(n) &gt; 0) {
           cout &lt;&lt; "Cache Hit" &lt;&lt; endl;
           ret = memo[n];
           return ret;
        }
        ret = foo(n);
        memo[n] = ret;
        return ret;
     };
  }
  
  // Recursive lambda fibonacci function
  std::function&lt;int(int) &gt; fibonacci_r = [](int n) {
     if (n &lt;= 1) {
        return n;
     } else {
        return fibonacci_r(n - 1) + fibonacci_r(n - 2);
     }
  };
  
  // Recursive lambda factorial function
  std::function&lt;int(int) &gt; factorial_r = [](int n) {
     if (n == 0) {
        return 1;
     } else {
        return n * factorial_r(n - 1);
     }
  };
  </code></pre>
  
  <p><strong>Logic For Testing Memoized Functions</strong></p>
  
  <pre><code>int position = 7;
  cout &lt;&lt; "Fibonacci:" &lt;&lt; endl;
  cout &lt;&lt; "Non Memo Fibonacci" &lt;&lt; endl;
  cout &lt;&lt; position &lt;&lt; "-&gt; " &lt;&lt; fibonacci_r(position) &lt;&lt; endl;
  cout &lt;&lt; "Memo Fibonacci" &lt;&lt; endl;
  fibonacci_r = memoize(fibonacci_r);
  cout &lt;&lt; position &lt;&lt; " -&gt; " &lt;&lt; fibonacci_r(position) &lt;&lt; endl;
  
  cout &lt;&lt; endl;
  
  cout &lt;&lt; "Non Memo Factorial" &lt;&lt; endl;
  cout &lt;&lt; position &lt;&lt; " -&gt; " &lt;&lt; factorial_r(position) &lt;&lt; endl;
  cout &lt;&lt; "Memo Factorial" &lt;&lt; endl;
  factorial_r = memoize(factorial_r);
  cout &lt;&lt; position &lt;&lt; " -&gt; " &lt;&lt; factorial_r(position) &lt;&lt; endl;
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Fibonacci:
  Non Memo Fibonacci
  7-&gt; 13
  Memo Fibonacci
  Cache Hit
  Cache Hit
  Cache Hit
  Cache Hit
  Cache Hit
  7 -&gt; 13
  
  Non Memo Factorial
  7 -&gt; 5040
  Memo Factorial
  Cache Hit
  7 -&gt; 13
  </code></pre>
  
  <p>At the tail end of the output you can see that the Memo factorial has a cache hit.  However, I don't think it should have only 1 cache hit.  Either way, <code>7!</code> isn't 13 and 13 <em>is</em> the cache value for 7 under the fibonacci memo lambda.</p>