Compare C++ Virtual Dispatch with CRTP

Quick Overview

Compare C++ virtual dispatch with CRTP across runtime flexibility, object layout, separate compilation, heterogeneous collections, inlining, code size, diagnostics, ABI stability, and measured performance.

Compare C++ Virtual Dispatch with CRTP

Company: Hudson River Trading

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

Explain how C++ virtual functions are commonly implemented and compare runtime polymorphism with the Curiously Recurring Template Pattern. Discuss object layout, dispatch, extensibility, code generation, and when each design is appropriate. ### Constraints & Assumptions - The C++ standard specifies behavior but not one mandatory vtable layout. - The comparison should include separate compilation and heterogeneous collections. - Do not claim that one indirect call is always a meaningful performance problem. ### Clarifying Questions to Ask - Must implementations be selected at runtime or are concrete types known at compile time? - Will objects be handled through one stable ABI across shared-library boundaries? - Is binary size or build time a material constraint? ### What a Strong Answer Covers - A qualified description of vptrs, vtables, overrides, and dynamic dispatch. - Virtual destructors and the danger of deleting through a base without one. - CRTP static dispatch and compile-time interface expectations. - Trade-offs involving runtime flexibility, inlining, code size, diagnostics, and ABI. - A requirement-based choice plus measurement rather than dogma. ### Follow-up Questions - How can devirtualization remove the indirect call? - Can CRTP store unrelated derived objects in one container without another abstraction? - When would type erasure be a better compromise?

Overview: Compare C++ virtual dispatch with CRTP across runtime flexibility, object layout, separate compilation, heterogeneous collections, inlining, code size, diagnostics, ABI stability, and measured performance.

Hudson River Trading logo
Hudson River Trading
May 5, 2025
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
3
0

Explain how C++ virtual functions are commonly implemented and compare runtime polymorphism with the Curiously Recurring Template Pattern. Discuss object layout, dispatch, extensibility, code generation, and when each design is appropriate.

Constraints & Assumptions

  • The C++ standard specifies behavior but not one mandatory vtable layout.
  • The comparison should include separate compilation and heterogeneous collections.
  • Do not claim that one indirect call is always a meaningful performance problem.

Clarifying Questions to Ask Guidance

  • Must implementations be selected at runtime or are concrete types known at compile time?
  • Will objects be handled through one stable ABI across shared-library boundaries?
  • Is binary size or build time a material constraint?

What a Strong Answer Covers Guidance

  • A qualified description of vptrs, vtables, overrides, and dynamic dispatch.
  • Virtual destructors and the danger of deleting through a base without one.
  • CRTP static dispatch and compile-time interface expectations.
  • Trade-offs involving runtime flexibility, inlining, code size, diagnostics, and ABI.
  • A requirement-based choice plus measurement rather than dogma.

Follow-up Questions Guidance

  • How can devirtualization remove the indirect call?
  • Can CRTP store unrelated derived objects in one container without another abstraction?
  • When would type erasure be a better compromise?