title: Does the (gcc) compiler optimize away empty-body functions?
body: <p>Using policy based design, an <code>EncapsulatedAlgorithm</code>: </p>
  
  <pre><code>template&lt; typename Policy&gt;
  class EncapsulatedAlgorithm : public Policy
  {
      double x = 0; 
  
      public: 
          using Policy::subCalculate; 
  
          void calculate()
          {
              Policy::subCalculate(x); 
          }
      protected:
          ~EncapsulatedAlgorithm() = default;
  };
  </code></pre>
  
  <p>may have a policy <code>Policy</code> that performs a sub-calculation. The sub-calculation is not necessary for the algorithm: it can be used in some cases to speed up algorithm convergence. So, to model that, let's say there are three policies. </p>
  
  <p>One that just "logs" something:</p>
  
  <pre><code>struct log
  {
      static void subCalculate(double&amp; x)
      {
          std::cout &lt;&lt; "Doing the calculation" &lt;&lt; endl; 
      }
  };
  </code></pre>
  
  <p>one that calculates: </p>
  
  <pre><code>struct calculate
  {
      static void subCalculate(double&amp; x)
      {
          x = x * x; 
      }
  };
  </code></pre>
  
  <p>and one to bring them all and in the darkness bind them :D - that does <em>absolutely nothing</em>:</p>
  
  <pre><code>struct doNothing
  {
      static void subCalculate(double&amp; x)
      {
          // Do nothing. 
      }
  };
  </code></pre>
  
  <p>Here is the example program:</p>
  
  <pre><code>typedef EncapsulatedAlgorithm&lt;doNothing&gt; nothingDone; 
  typedef EncapsulatedAlgorithm&lt;calculate&gt; calculationDone; 
  typedef EncapsulatedAlgorithm&lt;loggedCalculation&gt;  calculationLogged; 
  
  int main(int argc, const char *argv[])
  {
      nothingDone n; 
      n.calculate(); 
  
      calculationDone c; 
      c.calculate();
  
      calculationLogged l; 
      l.calculate(); 
  
      return 0;
  }
  </code></pre>
  
  <p>And <a href="http://coliru.stacked-crooked.com/a/be318b75153bf596" rel="nofollow">here</a> is the live example. I tried examining the assembly code produced by <code>gcc</code> with the optimization turned on:</p>
  
  <pre><code>g++ -S -O3 -std=c++11 main.cpp
  </code></pre>
  
  <p>but I do not know enough about Assembly to interpret the result with certainty - the resulting file was tiny and I was unable to recognize the function calls, because the code of the static functions of all policies was inlined. </p>
  
  <p>What I could see is that when no optimization is set for the, within the main function, there is a <code>call</code> and a subsequent <code>leave</code> related to the 'doNothing::subCalculate'</p>
  
  <pre><code>call    _ZN9doNothing12subCalculateERd
  leave
  </code></pre>
  
  <p>Here are my questions: </p>
  
  <ol>
  <li>Where do I start to learn in order to be able to read what <code>g++ -S</code> spews out? </li>
  <li>Is the empty function optimized away or not and where in <code>main.s</code> are those lines?</li>
  <li>Is this design O.K.? Usually, implementing a function that does nothing is a bad thing, as the interface is saying something completely different (<code>subCalculate</code> instead of <code>doNothing</code>), but in the case of policies, the policy name clearly states that the function will not do anything. Otherwise I need to do type traits stuff like <code>enable_if</code>, etc, just to exclude a single function call. </li>
  </ol>