Interactive Learning: Tutorials and Workshops¶
The mp-units documentation has two new sections: tutorials and hands-on workshops for learning type-safe physical quantities and units. The tutorials start at the first quantity you create, and the workshops go on to the more advanced patterns.
Interactive Code Examples¶
All tutorials and workshops use embedded interactive code editors powered by Compiler Explorer. Each code example is a live editor that compiles and runs automatically as you type:
- ✅ Edit and see results instantly: type code and watch it compile
- ✅ Get immediate feedback: compiler diagnostics appear as you code
- ✅ See program output: text output is shown directly below the editor
- ✅ Experiment freely: try variations without leaving the documentation
There is nothing to install and nothing to set up.
Experience it yourself
In the tutorials, every code example appears as an embedded interactive editor. You can modify the code directly in the documentation page and see it compile and run instantly. For example, a speed calculation tutorial might show:
// ce-embed height=400 compiler=clang2110 flags="-std=c++23 -stdlib=libc++ -O3" mp-units=trunk
#include <mp-units/systems/si.h>
#include <iostream>
int main()
{
using namespace mp_units;
using namespace mp_units::si::unit_symbols;
quantity distance = 100 * m;
quantity time = 9.58 * s;
quantity speed = distance / time; // Automatically derives m/s
std::cout << "Distance: " << distance << "\n";
std::cout << "Time: " << time << "\n";
std::cout << "Speed: " << speed << "\n";
}
Just edit the numbers or units in the embedded editor, and watch the output update automatically below the code.
Why the Examples Are Editable¶
The examples can be edited and run without leaving the page, so you can try the library while you read about it.
Immediate Experimentation¶
Try the embedded editor above:
- Change
distanceto200 * mand watch the speed update - Try adding an invalid operation like
distance + timeand see the compiler error instantly - Experiment with different units:
distance = 100 * kmstill works perfectly
The compiler diagnostic appears immediately when you make a mistake, and it shows why the dimensions don't match. There is no separate tool to run.
Learn from Mistakes¶
Making mistakes is part of learning. With embedded interactive editors:
- Type invalid operations and see compiler errors instantly
- Read compiler diagnostics in context as you code
- Fix errors and watch the program compile successfully
- Build intuition through immediate feedback
Active Learning¶
A few things worth doing while you read:
- Type the code yourself rather than only reading it
- Experiment with variations: change values and try different units
- Break things on purpose to find out where the boundaries are
- See the results immediately, with no compile and run cycle of your own
Getting Started¶
Where to start depends on what you already know:
For Beginners¶
- Start with Tutorials: complete all 17 lessons (~4 hours)
- Try the Foundation Workshops: practice refactoring patterns
- Explore Examples: study complete applications
- Dive into specific topics in the User's Guide as needed
For Experienced Developers¶
- Skim Quick Start tutorials (30 min)
- Focus on Type Safety tutorials (~1 hour)
- Jump to Customization and Advanced Workshops
- Reference the Systems documentation for your domain
For Migration¶
- Read the relevant Migration Guide
- Complete the Workshop: Incremental Migration
- Reference Interoperability guides as needed
Tutorials¶
The 17 step-by-step tutorials (~4 hours in total) start at "Hello World" and cover the core features of the library. Each tutorial is meant to:
- Build progressively: each lesson builds on the previous one
- Teach by example: through working code rather than abstract theory
- Provide instant feedback: interactive examples let you experiment immediately
- End with key insights: every code example concludes with "What You Learned?" takeaways
Learning Path¶
1. Quick Start (~30 minutes)¶
Three short lessons to get started:
- Your First Quantities: create and manipulate quantities with units
- Simple Math: perform calculations and see dimensional analysis in action
- Building a Calculator: assemble a real program putting concepts together
Who it is for
Developers who want to see what mp-units can do before going further.
2. Working with Units (~50 minutes)¶
Converting units and extracting values:
- Unit Conversions: convert seamlessly between compatible units
- Safe and Unsafe Conversions: handle truncation and understand explicit conversions
- Extracting Numeric Values: get numerical values safely when needed
Who it is for
Anyone who needs to work with multiple unit systems or integrate with legacy APIs.
3. Type Safety (~65 minutes)¶
How compile-time dimensional analysis works:
- Compile-Time Protection: watch the compiler prevent nonsensical operations
- Automatic Dimensional Analysis: let the compiler compute derived units
- Quantity Specifications: use quantity hierarchies for stronger type safety
- Generic Interfaces: write flexible, reusable code with
QuantityOf
Who it is for
Developers who want to eliminate runtime errors and write self-documenting code.
4. Affine Space (~90 minutes)¶
The distinction between points and quantities:
- Points and Quantities: learn when to use
quantity_pointvsquantity - Point Origins: work with multiple reference frames
- Temperature Handling: handle absolute vs relative temperatures correctly
- Temperature Applications: apply affine space concepts to real problems
- Bounded Quantity Points: attach domain constraints to origins
- Custom Contract Handlers: handle constraint violations the way a production system needs
- Frame Projections: convert between independent reference frames
Who it is for
Anyone working with temperatures, timestamps, GPS coordinates, or any absolute measurements.
Workshops¶
After the tutorials there are 15 workshops. Each one presents a coding challenge taken from a real-world scenario and requires you to apply concepts from the User's Guide to solve it.
How Workshops Differ from Tutorials¶
In a workshop:
- Exercise-driven: follow
TODO:markers to implement the solutions yourself - Real-world scenarios: production-like code rather than toy examples
- Progressive complexity: start with refactoring, advance to custom systems
- Self-directed learning: review the User's Guide to find the solutions
- Practical patterns: techniques you will use in actual projects
Workshop Progression¶
Foundation Workshops¶
Build essential skills through refactoring and practical patterns:
- Refactor to Strong Types: transform weak types into strongly-typed quantities
- Generic, Type-Safe Interfaces: make APIs flexible yet safe with
QuantityOf - Extracting Numeric Values: handle legacy interfaces requiring raw numbers
- Working with Temperatures: master absolute vs relative temperature handling
- Affine Spaces and Origins: work with multiple coordinate systems
- Interop with std::chrono: bridge between time libraries seamlessly
Who it is for
Integrating mp-units into existing codebases and working with legacy systems.
Extensions Workshops¶
Create domain-specific systems of quantities and units:
- Custom Dimensionless Units: create strongly-typed counters for business domains (cartons, pallets, trucks)
- Typed Quantities of Same Kind: distinguish height, width, and turn radius to prevent argument confusion
- Custom Quantity Specifications: define domain-specific energy types for elevator system design
- Custom Base Dimensions: create financial dimensions (shares, currency) for portfolio tracking
- Distinct Quantity Kinds: prevent mixing fluid head and water head in hydraulic engineering
Who it is for
Building specialized systems for your domain: aviation, robotics, finance, data engineering, etc.
Advanced Workshops¶
More advanced patterns:
- Bounds Validation: implement production-ready bounds validation with custom contract handlers
- Strongly-Typed Counts: use dimensionless quantities for graphics coordinates and buffer indices
- Faster-than-Lightspeed Constants: implement physical constants as compile-time units with automatic cancellation
- Incremental Migration: gradually adopt mp-units in legacy projects
Who it is for
Advanced users working on complex systems requiring maximum type safety.
Community and Feedback¶
We would love to hear from you:
- Questions? Join discussions in our GitHub Discussions
- Found an issue? Report it on GitHub Issues
- Success story? Share it on social media with
#mpunitsor#cplusplus - Suggestions? We are always looking to improve the learning resources
Help Us Improve¶
As you work through tutorials and workshops:
- Let us know if something is unclear
- Suggest additional exercises or topics
- Share patterns you discover
- Report broken Compiler Explorer examples
Try It Today¶
Open any tutorial or workshop and you will find interactive code editors embedded in the documentation. Start typing, and the code compiles automatically.
Special thanks to Matt Godbolt and the rest of the Compiler Explorer team for making interactive learning possible.