title: passing lambda as argument - by reference or value?
body: <p>I've written a template code that takes a functor as an argument and after some processing, executes it. Although someone else might pass that function a lambda, a function pointer or even an <code>std::function</code> but it is meant primarily for lambda(not that I ban other formats). I want to ask how should I take that lambda - by value? by reference? or something else.</p>
  
  <p>Example code -</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  using namespace std;
  
  template&lt;typename Functor&gt;
  void f(Functor functor)
  {
      functor();
  }
  
  void g()
  {
      cout &lt;&lt; "Calling from Function\n";
  }
  
  int main() 
  {
      int n = 5;
  
      f([](){cout &lt;&lt; "Calling from Temp Lambda\n";});
  
      f([&amp;](){cout &lt;&lt; "Calling from Capturing Temp Lambda\n"; ++n;});
  
      auto l = [](){cout &lt;&lt; "Calling from stored Lambda\n";};
      f(l);
  
      std::function&lt;void()&gt; funcSTD = []() { cout &lt;&lt; "Calling from std::Function\n"; };
      f(funcSTD);
  
      f(g);
  }
  </code></pre>
  
  <p>In above code, I've a choice of making it either of these -</p>
  
  <pre><code>template&lt;typename Functor&gt;
      void f(Functor functor)
  
  template&lt;typename Functor&gt;
      void f(Functor &amp;functor)
  
  template&lt;typename Functor&gt;
      void f(Functor &amp;&amp;functor)
  </code></pre>
  
  <p>What would be the better way and why? Are there any limitations to any of these?</p>