I've created cppmatch, a lightweight, header-only C++ library that brings Rust-inspired pattern matching and error handling to C++.<p>It tries to imitate the functionality of the questionmark (?) operator in C++ by using a macro that uses
the gcc extension <a href="https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html" rel="nofollow">https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html</a><p>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.<p>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)<p>You can view an example of this project used in a "real way" in compiler-explorer:<p>Simplified error types to just be a string: <a href="https://compiler-explorer.com/z/6j3866E7W" rel="nofollow">https://compiler-explorer.com/z/6j3866E7W</a><p>Multiple structs as error types: <a href="https://compiler-explorer.com/z/encbf5f43" rel="nofollow">https://compiler-explorer.com/z/encbf5f43</a><p>Feel free to give feedback or contribute to the project!