# 13eholder Modern Cpp Skills M05 Type Driven

> C++ Type-Driven Design

- Skill: `tomevault-io/13eholder-modern-cpp-skills-m05-type-driven` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/13eholder-modern-cpp-skills-m05-type-driven`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/13eholder-modern-cpp-skills-m05-type-driven/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/13eholder-modern-cpp-skills-m05-type-driven

---


# C++ Type-Driven Design

## Core Question

**Can I make this bug a compile error?**

- **Primitive Obsession**: Using `int` for IDs, `double` for Money. Bad.
- **Strong Types**: `struct UserId`, `struct Money`. Good.
- **Type State**: `Connection<OFF>` vs `Connection<ON>`.

## Error → Design Question

| Issue                 | Design Question                                               |
| --------------------- | ------------------------------------------------------------- |
| **Swapped arguments** | Did you pass `width` to `height`? (Use Strong Types).         |
| **Invalid State**     | Did you call `read()` on closed file? (Use Type State).       |
| **Unit confusion**    | Did you mix Meters and Feet? (Use `std::chrono` style units). |

## Thinking Prompt

1.  **Is this `int` unique?**
    - Yes? → Wrap in `struct`.
    - `struct UserId { int val; };` prevents `process(OrderId)`.

2.  **Does valid usage depend on order?**
    - Yes? → Encode state in type.
    - `Builder::port()` returns `BuilderWithPort`.

3.  **Are units compatible?**
    - No? → Template tag. `Dist<Meters>` + `Dist<Feet>`.

## Trace Up / Down

-   **Trace Up**:
    -   *Issue*: "Rocket crashed because of Metric vs Imperial confusion."
    -   *Cause*: `double calculate_trajectory(double dist)` accepted any number.
    -   *Fix*: `Dist<Meters> calculate(Dist<Meters> d)`. Compilation fails if you pass Feet.

-   **Trace Down**:
    -   *Intent*: "Ensure file is open before reading."
    -   *Code*: `File<Open> f = File<Closed>().open(); f.read();`

## Quick Reference

| Pattern            | Cost | Use When                                |
| ------------------ | ---- | --------------------------------------- |
| **Struct Wrapper** | Zero | Distinct IDs, coordinates.              |
| **Enum Class**     | Zero | Type-safe flags (no implicit int conv). |
| **Phantom Type**   | Zero | Tracking state without storage.         |
| **User Literal**   | Zero | `10_m`, `50_s`.                         |

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/13eholder) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-11 -->

