TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Cppmatch – Rust-Like Pattern Matching and Error Handling for C++

42 点作者 Rucadi大约 2 个月前
I&#x27;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:&#x2F;&#x2F;gcc.gnu.org&#x2F;onlinedocs&#x2F;gcc&#x2F;Statement-Exprs.html" rel="nofollow">https:&#x2F;&#x2F;gcc.gnu.org&#x2F;onlinedocs&#x2F;gcc&#x2F;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 &quot;real way&quot; in compiler-explorer:<p>Simplified error types to just be a string: <a href="https:&#x2F;&#x2F;compiler-explorer.com&#x2F;z&#x2F;6j3866E7W" rel="nofollow">https:&#x2F;&#x2F;compiler-explorer.com&#x2F;z&#x2F;6j3866E7W</a><p>Multiple structs as error types: <a href="https:&#x2F;&#x2F;compiler-explorer.com&#x2F;z&#x2F;encbf5f43" rel="nofollow">https:&#x2F;&#x2F;compiler-explorer.com&#x2F;z&#x2F;encbf5f43</a><p>Feel free to give feedback or contribute to the project!

4 条评论

nickysielicki大约 2 个月前
<a href="https:&#x2F;&#x2F;libfn.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;libfn.org&#x2F;</a> also is worth a look.
评论 #43395056 未加载
j1elo大约 2 个月前
This is great! Thanks for sharing, it is a cool idea to try bringing QoL improvements from Rust to C++. Got a question:<p>Does this prevent the RVO&#x2F;NRVO compiler optimizations for return values? And when those fail, same question for the move-construction for types that do have a move constructor.
评论 #43385908 未加载
senkora大约 2 个月前
Have you considered using the immediately invoked function expression (IIFE) pattern as a standards-compliant alternative to gcc statement expressions?
评论 #43385868 未加载
bestouff大约 2 个月前
Please leave that Frankenstein of a language alone. Stop bolting features on it, it&#x27;s already a monster.