title: C++ Nested Lambda
body: <p>I am working on a data aggregator implementation with nested lambda expressions. I am not very experienced with lambda functions yet and I am not 100% sure whether my implementation idea is implementable.</p>
  
  <p>The Problem: I have a <em>Multivalue</em> class which has a vector as a private member. The data type of this private vector can be defined via a template parameter of the <em>Multivalue</em> class. I want to offer different aggregation functions (sum, harmonic mean, average, ...) for the data in the vector. BUT: if the vector data type is a tuple, the aggregation functions should also be available for the components of the tuple. The following link shows my first try, which is not working yet. I hope someone can explain to me what the problem is. I think there is a problem with the nested lambda expressions, I use:</p>
  
  <p><a href="http://goo.gl/w7MUAi" rel="nofollow">http://goo.gl/w7MUAi</a></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;functional&gt;
  #include &lt;tuple&gt;
  
  using namespace std;
  
  template&lt;typename ... TTypes&gt;
  std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, const std::tuple&lt;TTypes...&gt;&amp; value) { return out; }
  
  /** our general abstract aggregation object */
  template&lt;typename T, typename C&gt;
  class AbstractAggregator {
  public: 
      AbstractAggregator() { }
      AbstractAggregator(const std::vector&lt;C&gt; *data, std::function&lt;const T&amp;(const C&amp;)&gt; access) :
      data(data), access(access) { }
  
      T sum() const {
          T sum;
          for (auto &amp;i : *data)
              sum += access(i);
          return sum;
      }
  protected:
      const std::vector&lt;C&gt; *data;
      std::function&lt;const T&amp;(const C&amp;)&gt; access;
  };
  
  /** concrete aggregation implementation for types like int, float, double .. */
  template&lt;typename T, typename C&gt;
  class Aggregator : public AbstractAggregator&lt;T, C&gt; {
  public:
      Aggregator() { }
      Aggregator(const std::vector&lt;C&gt; *data, std::function&lt;const T&amp;(const C&amp;)&gt; access) : AbstractAggregator&lt;T, C&gt;(data, access) { }    
  };
  
  /** aggregator implementation for tuple (with subaggregators for each component */
  template&lt;typename ... TTypes, typename C&gt;
  class Aggregator&lt;std::tuple&lt;TTypes...&gt;, C&gt; : public AbstractAggregator&lt;std::tuple&lt;TTypes...&gt;, C&gt; {
  public:
      Aggregator() { }
      Aggregator(const std::vector&lt;C&gt; *data, std::function&lt;const std::tuple&lt;TTypes...&gt;&amp;(const C&amp;)&gt; access) : AbstractAggregator&lt;std::tuple&lt;TTypes...&gt;, C&gt;(data, access) { 
      initSubAggregators&lt;sizeof...(TTypes), TTypes...&gt;(access); 
  }
  
      std::tuple&lt;Aggregator&lt;TTypes, C&gt;...&gt; subaggregators;
  private:
      template&lt;int N&gt;
      void initSubAggregators(std::function&lt;const std::tuple&lt;TTypes...&gt;&amp;(const C&amp;)&gt; access) { }
      template&lt;int N, typename THead, typename... TTail&gt;
      void initSubAggregators(std::function&lt;const std::tuple&lt;TTypes...&gt;&amp;(const C&amp;)&gt; access) { 
          constexpr int I = N - sizeof...(TTail) - 1;
          std::get&lt;I&gt;(subaggregators) = Aggregator&lt;THead, C&gt;(AbstractAggregator&lt;std::tuple&lt;TTypes...&gt;, C&gt;::data, [&amp;](const C &amp;value) { return std::get&lt;I&gt;(access(value)); });        
          initSubAggregators&lt;N, TTail...&gt;(access);
      }
  };
  
  namespace std {
      template&lt;size_t I, typename ... TTypes, typename C&gt;
      auto get(Aggregator&lt;std::tuple&lt;TTypes...&gt;, C&gt;&amp; k) -&gt; decltype(std::get&lt;I&gt;(k.subaggregators)) {
          return std::get&lt;I&gt;(k.subaggregators);
      }
  
      template&lt;size_t I, typename ... TTypes, typename C&gt;
      auto get(const Aggregator&lt;std::tuple&lt;TTypes...&gt;, C&gt;&amp; k) -&gt; decltype(std::get&lt;I&gt;(k.subaggregators)) {
          return std::get&lt;I&gt;(k.subaggregators);
      }
  }
  
  /** multivalue attribute implementation (inherits corresponding aggregation object) */
  template&lt;typename T&gt;
  class Multivalue : public Aggregator&lt;T, T&gt; {
  public:
      Multivalue() : Aggregator&lt;T, T&gt;(&amp;data, [](const T &amp;value) { return value; }) { }
      void append(const T&amp; item) { data.push_back(item); }
  private:
      std::vector&lt;T&gt; data;
  };
  
  int main() {
      Multivalue&lt;std::tuple&lt;std::tuple&lt;uint32_t, uint32_t&gt;, float, double&gt;&gt; mv;
      mv.append(std::make_tuple(std::make_tuple(13, 12), 2.5, 3.5));
      mv.append(std::make_tuple(std::make_tuple(1, 7), 2.55123, 1.5));
      mv.append(std::make_tuple(std::make_tuple(5, 3), 2.312, 1.8));
  
      auto &amp;a1 = std::get&lt;2&gt;(mv);
      auto &amp;a2 = std::get&lt;1&gt;(mv);
      auto &amp;a3 = std::get&lt;0&gt;(std::get&lt;0&gt;(mv));
      auto &amp;a4 = std::get&lt;1&gt;(std::get&lt;0&gt;(mv));
  
      std::cout &lt;&lt; "Sum 1: " &lt;&lt; a1.sum() &lt;&lt; std::endl;
      std::cout &lt;&lt; "Sum 2: " &lt;&lt; a2.sum() &lt;&lt; std::endl;
      std::cout &lt;&lt; "Sum 3: " &lt;&lt; a3.sum() &lt;&lt; std::endl;
      std::cout &lt;&lt; "Sum 4: " &lt;&lt; a4.sum() &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Moo</p>