title: What's the difference between using an -O flag and turning on the constituent optimizations individually?
body: <p>I have a program which compiles and runs properly when no <code>-O</code> flags are set.</p>
  
  <p>When I try building with <code>-Og</code>, the program complains of a pure virtual function being called and crashes.</p>
  
  <p>I was trying to track this down, so I used the command</p>
  
  <pre><code>g++ -Og  -Q --help=optimizers
  </code></pre>
  
  <p>to get a list of optimizations that <code>-Og</code> turns on.  This gave me a list of 83 different optimization flags that are supposedly enabled. (Curiously, I still get 58 optimizations if I supply <code>-O0</code> instead of <code>-Og</code>.)</p>
  
  <p>I tried building with this list of optimizations instead of <code>-Og</code> directly, with the idea that I'd use a binary search to find the offending optimizations.  To my surprise, what happened instead was that the program functioned correctly!</p>
  
  <p><strong>Question</strong>: Am I doing something wrong when I'm getting the list of enabled optimizations?  Or does the <code>-Og</code> flag have other effects?</p>
  
  <hr>
  
  <p><strong>Bonus details</strong></p>
  
  <p>Unfortunately I haven't been able to strip the program I'm compiling down to a minimal working example.  Whatever this is seems fairly delicate, and the problem goes away if, for instance, I add a <code>cout</code> inside of a constructor.  However, to give a basic sketch of what's going on here:</p>
  
  <ul>
  <li>There's a base class with a pure virtual method.</li>
  <li>The base-class constructor calls <code>std::bind(&amp;Base::virtual_method, this)</code> and provides this as a callback to another object.</li>
  <li>The derived class implements the virtual method.</li>
  <li>The derived class constructor has some member initializers but no code in the constructor body.</li>
  <li>If I put code in the constructor body that can't be optimized out, e.g. a <code>cout</code>, then everything works as expected.</li>
  </ul>
  
  <p>So I'm guess the part of the derived class constructor that populates the vtable pointer is being optimized out for some reason.  (I haven't manually checked the generated assembly because I haven't figured out how to do it yet.)</p>