title: How can a variadic template be used to generate a left-associative expression (aka left fold) in c++11?
body: <p>I would like to use a c++ template to aggregate (fold) multiple arguments using a binary operation. </p>
  
  <p>Such a template could be used as follows:</p>
  
  <p><code>fold&lt;add&gt;(100,10,5)</code> expands to <code>add(add(100, 10), 5)</code></p>
  
  <p>The particular expansion shown above is the "left fold". The expansion <code>add(100, add(10, 5))</code> is the "right fold".  Assuming that the <code>add</code> function performs simple integer addition, both right and left folds produce the same result, 115. </p>
  
  <p>But consider a function <code>div</code> that performs integer division (<code>div(a,b)=a/b</code>). In this case, associativity matters and the left and right folds produce different results:</p>
  
  <pre><code>fold_left&lt;div&gt;(100,10,5)  --&gt; div(div(100, 10), 5) --&gt; div(10, 5) --&gt;  2
  fold_right&lt;div&gt;(100,10,5) --&gt; div(100, div(10, 5)) --&gt; div(100, 2) --&gt; 50
  </code></pre>
  
  <p>It is straightforward to use a variadic template to produce the right-associative version (<code>fold_right</code>), but I have not been able to figure out how to produce the left-associative version (<code>fold_left</code>). The attempted implementation of <code>fold_left</code> below results in a compiler error:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template &lt;typename T&gt; using binary_op = T(*)(const T&amp; a, const T&amp; b);
  
  // The terminal (single-argument) cases of the variadic functions defined later. 
  template&lt;typename T, binary_op&lt;T&gt; Operation&gt; inline T fold_right(const T&amp; t) { return t; }
  template&lt;typename T, binary_op&lt;T&gt; Operation&gt; inline T fold_left(const T&amp; t) { return t; }
  
  // Combines arguments using right-associative operation
  // i.e. fold_right&lt;T,op&gt;(A,B,C) --&gt; op(A, op(B,C))
  template&lt;typename T, binary_op&lt;T&gt; Operation, typename ... Rest&gt; 
  inline T fold_right(const T&amp; t, Rest... rest) {
      return Operation(t, fold_right&lt;T, Operation&gt;(rest...));
  }
  
  // Combines arguments using left-associative operation
  // i.e. fold_left&lt;T,op&gt;(A,B,C) --&gt; op(op(A,B), C)
  template&lt;typename T, binary_op&lt;T&gt; Operation, typename ... Rest&gt; 
  inline T fold_left(Rest... rest, const T&amp; t) {
      return Operation(fold_left&lt;T, Operation&gt;(rest...), t);
  }
  
  inline int add(const int&amp; a, const int&amp; b) { return a+b; }
  inline int div(const int&amp; a, const int&amp; b) { return a/b; }
  
  int main() {
      std::cout &lt;&lt; fold_right&lt;int,div&gt;(100,10,5) //  (100 / (10 / 5))  = 50
                &lt;&lt; "\n"
                &lt;&lt; fold_left&lt;int,div&gt;(100,10,5)  //  Compiler error!
                &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>How can variadic templates be used (in c++11) to correcty implement <code>fold_left</code>?</p>
  
  <p>I think it essentially comes down to being able to "pop" the last argument off of a parameter pack, which I attempted to do in the <code>left_fold</code> template above, but as I said, this resulted in a compiler error.</p>
  
  <p>Note: I have used simple arithmetic operations and integers as an example in this question, but the answer(s) should be generic enough to handle aggregation of objects using an arbitrary function (assuming it returns the same type of object as its arguments).</p>
  
  <p>Note 2: For those familiar with c++17, <em><a href="https://en.cppreference.com/w/cpp/language/fold" rel="nofollow noreferrer">fold expressions</a></em> can be used to produce both left and right folds with binary operators. But these are not available in c++11.</p>
  
  <p>As a related question: The above templates require the type T to be explicitly specified, as in <code>fold_right&lt;int,div&gt;(...)</code>. Is there some way to formulate the template so that only the operation is required, e.g. <code>fold_right&lt;div&gt;(...)</code>. I would think the type <code>T</code> could be inferred, but I don't see a way to order the template arguments to put the <code>binary_op&lt;&gt;</code> first.</p>
  
  <p>Thanks!</p>