<p>In general, the compiler will create a <code>vtable</code> and virtual method calls are dispatched through it, i.e., there is one added level of indirection in calls.</p>
<p>But optimizing compilers do try to avoid this. This optimization is generally called &quot;devirtualization&quot;.
When and how this works very much depends on the compiler and code in question.
<a href="https://quuxplusone.github.io/blog/2021/02/15/devirtualization/" rel="nofollow noreferrer">Here is a nice blog post about it.</a></p>
<p>Some more resources:</p>
<p>Talk: <a href="https://www.youtube.com/watch?v=w0sz5WbS5AM#t=3090" rel="nofollow noreferrer">Matt Godbolt on speculative devirtualization in LLVM</a></p>
<p>Talk: <a href="https://www.youtube.com/watch?v=qMhV6d3B1Vk" rel="nofollow noreferrer">2016 LLVM Developers’ Meeting: P. Padlewski “Devirtualization in LLVM”</a></p>
<p>Talk: <a href="https://www.youtube.com/watch?v=Dt4UehzzcsE" rel="nofollow noreferrer">2018 LLVM Developers’ Meeting: P. Padlewski “Sound Devirtualization in LLVM”</a></p>
<p>Paper: <a href="https://sites.cs.ucsb.edu/%7Eckrintz/papers/devirt-ibmjit.pdf" rel="nofollow noreferrer">Ishizaki et al, 2000, &quot;A Study of Devirtualization Techniques
for a Java™ Just-In-Time Compiler&quot;</a></p>
<p>Paper: <a href="https://arxiv.org/pdf/2003.04228.pdf" rel="nofollow noreferrer">Padlewski et al, 2020, &quot;Modeling the Invariance of Virtual Pointers in LLVM&quot;</a></p>
Is C++ virtual function always resolved in run time?
<p>I have a question regarding the resolving timing of a C++ virtual function. From chapter OOP in C++ Primer, it mentioned that:</p>
<blockquote>
<p><strong>Calls to Virtual Functions May Be Resolved at Run Time</strong>
When a virtual function is called through a reference or pointer, the compiler
generates code to decide at <strong>run time</strong> which function to call. The function that is called
is the one that corresponds to the dynamic type of the object bound to that pointer or
reference.</p>
</blockquote>
<p>I understand what the above statement describes: when a virtual function is executed, which version is really resolved depends on the actual type of calling pointer/reference. If the actual type of pointer/reference is base class, the virtual function of base class is the one actually being run, vice versa. It obviously needs to be done in run time.</p>
<p>However, the example following by the above statement in C++ primer has confused me for a while:</p>
<pre><code>double print_total(ostream &amp;os, const Quote &amp;item, size_t n)
{
  // depending on the type of the object bound to the item parameter
  // calls either Quote::net_price or Bulk_quote::net_price
  double ret = item.net_price(n);
}

// Quote is base class, Bulk_quote is derived class
Quote base(&quot;0-201-82470-1&quot;, 50);
print_total(cout, base, 10); // calls Quote::net_price
Bulk_quote derived(&quot;0-201-82470-1&quot;, 50, 5, .19);
print_total(cout, derived, 10); // calls Bulk_quote::net_price
</code></pre>
<p>My questions are:</p>
<ol>
<li>For my understanding, in this example, compiler is able to know in <strong>compile time</strong> the &quot;real type&quot; of <strong>instance base</strong> and <strong>instance derived</strong> as they are just declared obviously in sketch! So, I think the resolved timing of this example can be in compile time. Am I right about that?</li>
<li>Can resolved timing of virtual functions be compile time? Or as a matter of convenience, C++ just makes all virtual functions resolved in run time? Since C++ primer says: Calls to Virtual Functions <strong>May</strong> Be Resolved at Run Time, I am not quite sure if run time is all the case.</li>
</ol>
<p>I think to really understand the resolved time of virtual functions is very important to every C++ user. I tried to find  the knowledge about compile time/run time but none of them can help me figure out my question. Do anyone have any thoughts on my questions. Thank you in advance!</p>