title: Lazy evaluation of apply function implemented with variate template in C++
body: <p>I'm trying to write my own apply function using variate template. Here is the code I have:</p>
  
  <pre><code>    template&lt;typename...&gt;
      struct types{
      };
  
      template&lt;typename T, typename... Args&gt;
      struct Func{
          template&lt;typename F, typanem... Types&gt;
          T call(F f, std::tuple&lt;Types...&gt;&amp; params, Args... args){
              return _call(f, params, types&lt;Types...&gt;{}, args...);
          }
          template&lt;typename F, typename Tuple, typename Head, typename... Tail, typename... Args&gt;
          T _call(F f, Tuple&amp; t, types&lt;Head, Tail...&gt;, Args&amp;&amp;... args){
              _call(f, t, types&lt;Tail...&gt;{}, std::forward&lt;Args&gt;(args)..., std::get&lt;sizeof...(Arg)&gt;(t).DrawSample()));
          }
          T _call(F f, Tuple&amp; t, types&lt;&gt;, Args... vs){ 
              return f(vs...)
          }
      };
  </code></pre>
  
  <p>I can compile and run the above code without any difficulty. Now I want to make the function call(...) being invoked lazily. In other words, I can first construct the function by passing in all parameters needed for the computation:</p>
  
  <pre><code>    int getMax(float f1, float f2, float f3){
        if(f1 &gt;= f2 &amp;&amp; f1 &gt;= f3){
            return 1;
        }
        if(f2 &gt;= f1 &amp;&amp; f2 &gt;= f3){
            return 2;
        }
        if(f3 &gt;= f1 &amp;&amp; f3 &gt;= f2){
            return 3;
        }
      }
      Func&lt;int&gt; func;
      //Gaussian(mu, sd) is an object which can generate random values based on Gaussian 
      //distribution described with mu(mean) and sd(standard deviation).
      Gaussian *g1 = Gaussian(0.3, 0.2);
      Gaussian *g2 = Gaussian(0.2, 0.3);
      Gaussian *g3 = Gaussian(0.3, 0.3);
      func.setCall(getMax, std::make_tuple(*g1, *g2, *g3);
      cout&lt;&lt;func.DrawSample();
  </code></pre>
  
  <p>After constructing the function, I hope to get the result of the getMax() function lazily each time I make a query for func.DrawSample(). Under the hood, perhaps DrawSample() calls call(...). However, is there anyway for me to change the code above in order to create a setCall(...) whose purpose is to store everything needed for later function calls?</p>
  
  <p>Please let me know if my question is still unclear. Thanks a lot for your help!</p>