this post was submitted on 11 Nov 2023
19 points (88.0% liked)

C++

1773 readers
4 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] porgamrer@programming.dev 3 points 1 year ago* (last edited 1 year ago) (2 children)

I tried coming up with a decent Result type and Try macro in C++ and got nowhere. To be usable I need something like this:

auto varname = Try(expression);

Where the Try expression will either return the error type immediately or evaluate to the success type.

As far as I can tell there's no portable way to implement that macro in C++20, and nothing coming down the pipe either. I don't think chaining lambdas is good enough. It can make the code uglier than just manually checking returns.

I'll be happy if anyone can correct me though. The best I managed was still pretty gross:

TryBind(varname, expression);

Works, but it just looks like a bad DSL at that point.

[–] QuadriLiteral@programming.dev 2 points 1 year ago (1 children)

BOOST_OUTCOME_TRYX may be what you're looking for. It's only available on gcc and clang though.

[–] porgamrer@programming.dev 3 points 1 year ago

Yeah, I came across that. I presume it just uses the GCC statement expressions extension under the hood.

No can do though, it has to work with MSVC too.