When can the C++ compiler devirtualize a call?
4 days ago
- #Virtual function dispatch
- #Compiler behavior
- #C++ optimization
- The C++ compiler can devirtualize calls reliably for final methods, with variations across compilers in handling corner cases.
- Devirtualization occurs when the dynamic type is known (e.g., direct object usage or via dataflow analysis) or when a proof of leafness exists (e.g., final class/method, internal linkage).
- Compilers differ in their ability to devirtualize; GCC, Clang, and MSVC handle some cases well, while ICC lags, and each has unique strengths and weaknesses in optimization scenarios.