m05-type-driven

Enforce type safety in C++ code with strong types and type-state patterns.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

C++ code often relies on primitive types (int, double) for IDs or monetary values, which can lead to accidental misuse and bugs. Type-driven design introduces strong types, phantom types, and type-state patterns to encode invariants in the type system, reducing errors and improving API safety.

Core Features & Use Cases

  • Strong types to prevent mixing domains (e.g., UserId vs OrderId).
  • Phantom types to encode metadata without storage.
  • Type-state patterns and builders to enforce valid sequences at compile time.
  • Guided API design examples for resource handles, file operations, and domain models.

Quick Start

Start by wrapping primitive IDs with small structs and define a simple builder to enforce valid construction at compile time.

Frequently Asked Questions about m05-type-driven

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

FAQPage Schema
How do I prevent mixing different integer IDs like UserId and OrderId in C++?

Strong types prevent mixing different integer IDs like UserId and OrderId in C++ by wrapping primitive values in distinct structs. This type-driven design encodes domain boundaries directly into the type system, catching accidental misuse at compile time before runtime bugs occur.

What are phantom types and how do they work in C++?

Phantom types in C++ are template parameters used to encode metadata without adding runtime storage. They allow the type system to track state or domain distinctions, ensuring that only valid operations compile while keeping the generated code zero-cost.

Can I enforce valid state transitions at compile time in C++?

You can enforce valid state transitions at compile time in C++ using the type-state pattern and builder patterns. By encoding states as distinct types, the compiler restricts invalid sequences, ensuring resources or file operations follow valid construction steps.

Does type-driven design require external libraries for C++ APIs?

Type-driven design does not require external libraries for C++ APIs. It relies entirely on native templates and type design to achieve compile-time guarantees, meaning you can enforce safety for domain models and resource handles with zero external runtime dependencies.

What is the best way to protect floating point monetary values from bugs in C++?

The best way to protect floating point monetary values from bugs in C++ is applying strong types. Wrapping doubles in dedicated structs prevents accidental domain mixing and arithmetic errors, enforcing API safety through template-driven compile-time checks.

When should I not use type-state patterns for C++ design?

You should avoid type-state patterns in C++ design when state transitions are highly dynamic or determined at runtime. Because type-state relies on compile-time template enforcement, runtime-dependent states cannot be fully validated by the type system and require alternative logic.