m04-zero-cost

Guide C++20 developers in choosing static or dynamic polymorphism.

14|3|Updated Jan 25, 2026
One-click install
npx skills add https://github.com/13eholder/Modern-Cpp-Skills --skill m04-zero-cost-13eholder
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m04-zero-cost
Source: https://github.com/13eholder/Modern-Cpp-Skills/tree/main/m04-zero-cost
Command: npx skills add https://github.com/13eholder/Modern-Cpp-Skills --skill m04-zero-cost-13eholder

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps C++ developers decide between static and dynamic polymorphism to minimize runtime overhead, avoid object slicing and template/linker pitfalls, and balance binary size versus flexibility.

Core Features & Use Cases

  • Static dispatch guidance: When to use templates, Concepts, and CRTP for zero runtime cost and maximal optimization.
  • Dynamic dispatch guidance: When to use virtual functions, std::variant, std::function, or type-erasure for runtime flexibility and plugin-style extensibility.
  • Practical scenarios: Use templates or std::variant for closed sets and high-performance code, prefer virtual interfaces for runtime-extensible APIs and plugin systems, and apply Concepts to constrain templates and prevent template spew and misuse.

Quick Start

Prefer templates or CRTP for static dispatch when types are known at compile time, and switch to virtual functions or heap-polymorphism when runtime extensibility or heterogeneous containers are required.

Frequently Asked Questions about m04-zero-cost

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose between static and dynamic polymorphism in C++ to minimize runtime overhead?

To minimize runtime overhead with C++ polymorphism, choose static dispatch via templates, Concepts, or CRTP when types are known at compile time, and switch to virtual functions or std::variant when runtime extensibility or heterogeneous containers are required.

What's the best way to use std::variant for high performance C++ polymorphism?

For high performance C++ polymorphism, std::variant is best applied to closed sets of types in performance-sensitive code, offering a flexible alternative to virtual functions by avoiding vtable overhead while handling heterogeneous data efficiently.

When should I prefer CRTP over virtual functions in C++?

You should prefer CRTP over virtual functions in C++ when compile-time type knowledge is available, enabling zero-cost static polymorphism that maximizes compiler optimization and avoids the runtime dispatch overhead inherent to dynamic interfaces.

Does applying static polymorphism with C++ templates cause code bloat?

Applying static polymorphism with C++ templates can cause code bloat and linker pitfalls due to type-specific instantiations, so this Skill helps quantify these binary size costs against runtime flexibility to find the optimal dispatch pattern.

How do I use C++20 Concepts to prevent template spew in polymorphic code?

You use C++20 Concepts to constrain templates in polymorphic code, which prevents template spew and misuse by explicitly defining type requirements, thereby guiding static dispatch and improving compiler error messages during high performance development.