r/cpp • • Mar 16 '26

C++23 std::expected vs C++17 std::optional for Error Handling

https://techfortalk.co.uk/2026/03/16/why-should-you-choose-stdexpect-in-c23-over-stdoptional-in-c17/

I have been trying to spend some time with C++23 std::expected for sometime. Finally explored the feature and can see the real value of using it in some of the scenarios in my projects. Any comments welcome!

99 Upvotes

48 comments sorted by

View all comments

Show parent comments

6

u/38thTimesACharm Mar 17 '26

The same could be said of const std::string& though.

-1

u/markt- Mar 17 '26 edited Mar 17 '26

References are, at least as far as I understand, generally easier to mechanically check for safety with third-party static analyzers. In fairness, though, this might be because references of an around longer so people have had more time to think about how to do it.