Virtual dispatch in C++ enables runtime polymorphism but introduces overhead through vtable indirection, larger object sizes, and missed inlining opportunities. Compilers can sometimes devirtualize calls automatically using flags like -fwhole-program and -flto, or via the final keyword. When devirtualization isn't possible, static polymorphism via CRTP (Curiously Recurring Template Pattern) resolves calls at compile time with zero runtime cost. C++23's 'deducing this' feature offers a cleaner alternative to CRTP by templating only the member function rather than the entire class, achieving the same optimized output without the boilerplate.

6m read timeFrom david.alvarezrosa.com
Post cover image
Table of contents
Virtual dispatch §Devirtualization §Static polymorphism §

Sort: