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
Is this int unique?
Yes? → Wrap in struct.
struct UserId { int val; }; prevents process(OrderId).
Does valid usage depend on order?
Yes? → Encode state in type.
Builder::port() returns BuilderWithPort.
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 — claim your Tome and manage your conversions.
1---2name: 13eholder-modern-cpp-skills-m05-type-driven3description: C++ Type-Driven Design4---56# C++ Type-Driven Design78## Core Question910**Can I make this bug a compile error?**1112- **Primitive Obsession**: Using `int` for IDs, `double` for Money. Bad.13- **Strong Types**: `struct UserId`, `struct Money`. Good.14- **Type State**: `Connection<OFF>` vs `Connection<ON>`.1516## Error → Design Question1718| Issue | Design Question |19| --------------------- | ------------------------------------------------------------- |20| **Swapped arguments** | Did you pass `width` to `height`? (Use Strong Types). |21| **Invalid State** | Did you call `read()` on closed file? (Use Type State). |22| **Unit confusion** | Did you mix Meters and Feet? (Use `std::chrono` style units). |2324## Thinking Prompt25261. **Is this `int` unique?**27 - Yes? → Wrap in `struct`.28 - `struct UserId { int val; };` prevents `process(OrderId)`.29302. **Does valid usage depend on order?**31 - Yes? → Encode state in type.32 - `Builder::port()` returns `BuilderWithPort`.33343. **Are units compatible?**35 - No? → Template tag. `Dist<Meters>` + `Dist<Feet>`.3637## Trace Up / Down3839- **Trace Up**:40 - *Issue*: "Rocket crashed because of Metric vs Imperial confusion."41 - *Cause*: `double calculate_trajectory(double dist)` accepted any number.42 - *Fix*: `Dist<Meters> calculate(Dist<Meters> d)`. Compilation fails if you pass Feet.4344- **Trace Down**:45 - *Intent*: "Ensure file is open before reading."46 - *Code*: `File<Open> f = File<Closed>().open(); f.read();`4748## Quick Reference4950| Pattern | Cost | Use When |51| ------------------ | ---- | --------------------------------------- |52| **Struct Wrapper** | Zero | Distinct IDs, coordinates. |53| **Enum Class** | Zero | Type-safe flags (no implicit int conv). |54| **Phantom Type** | Zero | Tracking state without storage. |55| **User Literal** | Zero | `10_m`, `50_s`. |5657---58> Converted and distributed by [TomeVault](https://tomevault.io/claim/13eholder) — claim your Tome and manage your conversions.59<!-- tomevault:4.0:skill_md:2026-04-11 -->
Run npx skillmds@latest add tomevault-io/13eholder-modern-cpp-skills-m05-type-driven in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
C++ Type-Driven Design It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.