r/cpp • u/Clean-Upstairs-8481 • 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!
101
Upvotes
47
u/seido123 Mar 16 '26
Instead of `const string&` use `std::string_view` or have a good reason not to.
3
u/fdwr fdwr@github 🔍 Mar 20 '26
I like
std::expected, and at first I went a little overboard in using it, but then I realized there are some cases where just returning an optional or even a tuple of value+error (where value is set to a default value on error) made usage cleaner at the call site. In other cases I realized that what I really needed was something with more context that includes the full error chain (not just the most recent error). So I still haven't found a one-error-paradigm-that-fits-all, but I'm gladexpectedexists now as another readily available tool.2
u/programgamer Mar 17 '26
Neat overview but please proofread your writing in the future, this article is crawling with typos.
2
u/hamburgeraubacon Mar 18 '26
You're missing a huge part of
std::expected:and_then,or_else,tranformandtransform_errorwhich allow you to chain multipleexpectedin a very neat way !31
u/ukezi Mar 17 '26
I use both, optional for when a failure can be normal operation, like searching a map. Expected is for stuff where I can do something about failure, or at least want to log it, like if a network connection fails.
2
u/ponays Mar 17 '26
Useless comment on me but you have a typo : "returns the corresponding value (age) that is associuated with it"
1
u/Clean-Upstairs-8481 Mar 17 '26
Thanks a lot everyone for your valuable comments, couldn't look into all of those yet, but will look and fix some of the things mentioned here. Cheers
5
u/Mateh96 Mar 17 '26
You can always use both options depending on use case, you can even go further and use it along return bool and exceptions. Select best tool for given situation.
20
u/Electronic_Tap_8052 Mar 16 '26 edited Mar 17 '26
...
I tend to disagree with the verbiage here. Optional implies just that - something that's optional, i.e. that its not a failure if nullopt returns. Optional things don't need an explanation for why they didn't happen. If it's not ok if something happens, implying an exceptional case, then you should probably be using exceptions
1
u/Ericakester Mar 17 '26
We've been using our own standard compliant implementation of std::expected for years. It's a fantastic replacement for exceptions. We primarily use it with the proposed std::error from P0709 to pass along values/errors in our future library based on P1054
1
u/Specialist_Nerve_420 Mar 22 '26
optional and expected are just solving diff problems , optional is more like maybe there is a value, maybe not .
0
u/ignorantpisswalker Mar 17 '26
Myself I don't like the implementation. You sometimes use a "." and sometimes "->". This breaks my expectations and I don't know if its a object or pointer.
It just looks iffy to me.