title: C++11 Optimization of empty function passed as a callback
body: <p>I have a function template:</p>
  
  <pre><code>template &lt;class ReportFunc&gt;
  void func (ReportFunc report_func)
  {
      for (/* ... */)
      {
           do_something (a, b);
           report_func (a, b, c);
           do_something_else (b, c);
      }
  }
  </code></pre>
  
  <p>It is sometimes desirable to call func() without any ReportFunc, i.e. the loop just calls do_something() and do_something_else() and nothing else. If I write an overload of f() which doesn't take a ReportFunc parameter, I'll have to duplicate f()'s implementation code, just removing the line which calls report_func().</p>
  
  <p>I have several functions of this kind - sometimes I want to call them with a ReportFunc, sometimes without it. So I'd like to avoid all the code duplication. If I pass an empty lambda or void or something like this, should it make a C++11 compiler generate an instantiation of f() which doesn't call any report_func()? And is it as fast as simply deleting the line which calls report_func() or even an empty lambda has some overhead the compiler doesn't optimize? (in my specific case, I use GCC)</p>
  
  <p>Also: if an empty lambda indeed does that, and I change the function f()'s return type to be ReportFunc, i.e. it returns the report_func argument, is it still safe to store the returned value in a variable and call it? (even if it's an empty lambda? so calling it is theoretically possible, it simply means nothing really happens)</p>