I'm trying to write my own apply function using variate template. Here is the code I have:

```
    template<typename...>
    struct types{
    };

    template<typename T, typename... Args>
    struct Func{
        template<typename F, typanem... Types>
        T call(F f, std::tuple<Types...>& params, Args... args){
            return _call(f, params, types<Types...>{}, args...);
        }
        template<typename F, typename Tuple, typename Head, typename... Tail, typename... Args>
        T _call(F f, Tuple& t, types<Head, Tail...>, Args&&... args){
            _call(f, t, types<Tail...>{}, std::forward<Args>(args)..., std::get<sizeof...(Arg)>(t).DrawSample()));
        }
        T _call(F f, Tuple& t, types<>, Args... vs){ 
            return f(vs...)
        }
    };

```

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:

```
    int getMax(float f1, float f2, float f3){
      if(f1 >= f2 && f1 >= f3){
          return 1;
      }
      if(f2 >= f1 && f2 >= f3){
          return 2;
      }
      if(f3 >= f1 && f3 >= f2){
          return 3;
      }
    }
    Func<int> 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<<func.DrawSample();

```

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?

Please let me know if my question is still unclear. Thanks a lot for your help!
['https://Stackoverflow.com/questions/19552059', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2913077/']
Your problem is, that without braces, the `if` applies *only* to the following statement:

```
 if (q >= 0)
  e[q] = w;
  r[q] = w;  // <---- here you get ArrayIndexOutOfBound Exception

```

add braces like:

```
if (q >= 0){
  e[q] = w;
  r[q] = w;
}

```
Put the opening and closing braces for the if condition.