I've created cppmatch, a lightweight, header-only C++ library that brings Rust-inspired pattern matching and error handling to C++.
It tries to imitate the functionality of the questionmark (?) operator in C++ by using a macro that uses the gcc extension https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
This allows to create exceptionless code with non-intrusive error-as-value that unlike Exceptions, makes it clear which kinds of error a function can generate and forces you to handle (or ignore) them.
The ? operator translates to *expect* To handle the errors I introduce *match* which allows to easily visit the contents of the result or any std::variant (you can use it to imitate rust enums)
You can view an example of this project used in a "real way" in compiler-explorer:
Simplified error types to just be a string: https://compiler-explorer.com/z/6j3866E7W
Multiple structs as error types: https://compiler-explorer.com/z/encbf5f43
Feel free to give feedback or contribute to the project!
Comments URL: https://news.ycombinator.com/item?id=43380487
Points: 12
# Comments: 0
Login to add comment
Other posts in this group
Article URL: https://kevinalbs.com/checkers/
Comments URL: https://news.ycombinator.com

Article URL: https://discuss.systems/@dev/114161826926246661

Article URL: https://www.bbc.com/news/articles/cz03gjnxe25o
