title: Is it possible to pass generic lambda as non-template argument
body: <p>I have a toy example that I'd like to modify architecturally to remove type dependency of <code>Processor</code> on <code>EmitterT</code>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;utility&gt;
  
  using namespace std;
  
  struct Emitter {
      void e(int) { cout &lt;&lt; "emitting int\n";}
      void e(double) { cout &lt;&lt; "emitting double\n";}
      void e(char*) { cout &lt;&lt; "emitting char*\n";}
      void e(const char*) { cout &lt;&lt; "emitting const char*\n";}
  };
  
  template &lt;typename EmitterT&gt;
  struct Processor {
  
      Processor(EmitterT e) : e_{e} {}
  
      template &lt;typename T&gt;
      void process(T&amp;&amp; value) {
          cout &lt;&lt; "some processing... ";
          e_(std::forward&lt;T&gt;(value));
      }
  
      EmitterT e_;
  
  };
  
  template&lt;typename Emitter_&gt;
  Processor&lt;Emitter_&gt; makeProcessor(Emitter_ e) { return Processor&lt;Emitter_&gt;(e);}
  
  int main() {
      Emitter em;
      auto p = makeProcessor([&amp;em](auto v){em.e(v);});
  
  
      p.process(1);
      p.process("lol");
      return 0;
  }
  </code></pre>
  
  <h3>Motivation</h3>
  
  <p>I'd like to decouple part responsible for utilizing results of processing from the processing itself. The <code>Emitter</code> class structure is given to me, so I have to support overloaded functions.</p>
  
  <p>I'd like to pass a lambda function to a processor that will use it. Kind of like a callback mechanism, however it must be a generic lambda, to support overloads.</p>
  
  <h3>What I've tried:</h3>
  
  <p>The example I wrote works, but it depends on <code>Emitter</code> type as a template parameter. I don't like <code>Processor</code> type to change based on <code>Emitter</code>. It's also contagious, I have a real <code>Processor</code> hierarchy and <code>Emitter</code> spread like <code>const</code> or worse.</p>
  
  <p>After reading <a href="https://stackoverflow.com/a/17233649/1133179">https://stackoverflow.com/a/17233649/1133179</a> I've tried playing with below struct as a member:</p>
  
  <pre><code>struct EmitterC {
      template&lt;typename T&gt;
      void operator()(T value) { }
  };
  </code></pre>
  
  <p>But I cannot figure out a way to defer implementation of <code>Emitter</code> after <code>Processor</code> when using it as a normal parameter. It worked out with a forward declaration and a reference <code>EmitterC&amp;</code> but it supports one only Emitter definition. The only way I could come up with was to drop lambda, and make virtual overloads in <code>EmitterC</code> for every type I expect in <code>Emitter</code> and use it as a base class.</p>
  
  <p><strong>So, Is there a way to pass the (generic) lambda as a parameter, so that <code>Processor</code> type doesn't depend on <code>Emitter</code>?</strong></p>
  
  <p>I am restricted to c++14, but I am interested in more modern standards as well if the have better support.</p>