Show HN: Cppmatch – Rust-Like Pattern Matching and Error Handling for C++
Published on: 2025-06-14 22:07:03
cppmatch
A lightweight header-only Clang/GCC compatible C++ library for expressive pattern matching and error handling.
Check an example in examples folder or go to compiler-explorer to play with it!
Overview
A header-only C++ library that offers exceptionless error handling and type-safe enums, bringing Rust-inspired error propagation with the ? operator and the match operator to C++.
The expect macro simplifies error propagation by automatically returning the error from a function when an error (Err) is encountered, and it continues with the success value (Ok) otherwise. This results in code that is both safe and easy to read without unnecessary verbosity.
It allows to defer the error-checking to the point where it is really important while ensuring that all the error options are handled.
Types
A type alias for std::variant , representing either a success value of type T (index 0) or an error value of type E (index 1).
Example: cppmatch::Result< int , std::string> r =
... Read full article.