title: Compiler Optimization of virtual function calls
body: <p>Most popular example to illustrate why virtual dispatch happens at runtime is when it cannot be determined at compile time which Derived class is going to be created. For example:</p>
  
  <pre><code>Base* b = (rand() % 2 == 1 ? new Derived1() : new Derived2());
  </code></pre>
  
  <p>or when it depends on user input.</p>
  
  <p>Suppose none of that is the case and it can be completely determined at compile time which Derived class the base pointer is referring to.</p>
  
  <p>If it is known at compile time which Derived class the base class pointer points to, <strong>does the compiler optimize the virtual function call</strong> by substituting it with appropriate Derived function and not doing vtable lookup during runtime?</p>