r/cpp • u/javascript • May 09 '26
Any good tech talks leveraging statement expressions?
Lambdas do a great job in a lot of cases but sometimes you need a statement expression. Any good content on youtube?
14
Upvotes
View all comments
Show parent comment
2
u/javascript May 09 '26
As pointed out in another comment, do expressions have been proposed.
```
auto Bar() -> absl::StatusOr<MyType>;
auto Foo() -> absl::Status {
MyType mt = do {
absl::StatusOr<MyType> res = Bar();
if (res.ok()) do return std::move(res).value();
return std::move(res).status();
};
// Do stuff with valid MyType
return absl::OkStatus();
}
```
I think it's somewhat useful for beginners to see this code stamped out, but long term I really want something that does it for me. The `MY_TRY` macro is decent enough. I would perhaps prefer a keyword that you use as a suffix with a period. Perhaps `MyType mt = Bar().try;`.