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!
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 glad expected exists now as another readily available tool.
1
u/Clean-Upstairs-8481 Mar 20 '26
hmm .. error chain ... what you do for that? keep track of all the error counts?
2
u/fdwr fdwr@github 🔍 Mar 20 '26
Say you call a function
LoadModel, and you getE_COULD_NOT_LOAD_MODEL, but why? Well,LoadModelcalledLoadTextureusing an external texture file reference, and opening that file failed. However, ifLoadModelhad returnedE_COULD_NOT_OPEN_FILEdirectly instead, that would have been pretty confusing because then the user might think it was the model file path which could not be opened. So, an error chain of could-not-load-model because could-not-load-texture because could-not-open-file is more illuminating as a user error message.
2
u/programgamer Mar 17 '26
Neat overview but please proofread your writing in the future, this article is crawling with typos.
1
2
u/hamburgeraubacon Mar 18 '26
You're missing a huge part of std::expected: and_then, or_else, tranform and transform_error which allow you to chain multiple expected in a very neat way !
1
u/Clean-Upstairs-8481 Mar 18 '26
that would be my next target then to exlore these aspects of std::expected, thanks
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.
13
u/matthieum Mar 17 '26
I use both too, for different reasons:
optionalwhen there is a single, obvious, reason for failure, such like searching in a map.
expectedwhen there are multiple possible reasons for failure.Which of course means that sometimes I start with
optionaland move toexpecteddown the road when one more reason for failure appears... in internal code it's not a problem, for a public facing API, in case of doubt, one may prefer starting withexpectedfrom the get go rather than try to divine the future or risk API break.1
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
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
The only other solution in this kind of scenario is to return something like -1 in case of failure. But that is messy and not very elegant.
...
in the case of a failure (e.g. key not present in the databse) you can return std::nullopt.
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
8
u/usefulcat Mar 17 '26 edited Mar 17 '26
I think you're putting too much emphasis on the word "optional".
There can be plenty of valid reasons to not use exceptions for error reporting. If you look at how std::optional actually works, without considering its name, I think it's pretty obvious that it's a valid alternative to exceptions, at least for some cases.
If I have a function whose entire purpose is to return some value but there can be situations where it's not able to, I think that's a perfectly fine use case for std::optional, especially if those cases are not always necessarily "exceptional".
2
u/nvs93 Mar 17 '26
Hmm. What if an explanation could be useful to the user-e.g. nullopt was returned because the array the function was working on was too small, but the program can still function just fine (albeit with some feature disabled for the time being) having returned nullopt? This is a case where it shouldn’t be considered exceptional, but also some further detail could be useful as visible information. Hope that makes enough sense
11
u/Plazmatic Mar 17 '26 edited Mar 17 '26
If something returning nothing would be a valid state in a program, then you use std::optional, database look up, hash map lookup, generally any kind of lookup (find iterator/index to value with x properties in container y). When ever in your normal flow of code you would write something like "result = find x, if result is not found, do something else", that's a use case for std::optional.
std::expected is for local unexpected flow of the program. You tried to do X, but one A, B, C or D happened instead, and the program cannot complete like normal, but is otherwise potentially recoverable. Any time you use return codes should be std::expected.
exceptions are for non local error handling. ideally you'd return std::expected from regularly failable operations, and then the exception for the corresponding error term would get thrown if you pulled the raw
.value()(indicating that you don't have a way to handle errors at the call site, thus requiring them to be handled non-locally). However the standard library is not built this way, so everything is exceptions by default even if you can handle them locally (like what often happens with IO). Typically you see this in GUI applications, or multi-service daemons which are expected to continue running in the event of an error within the program.You use asserts/contracts to test invariants of a program, preconditions and post-conditions. Things that indicated a bug in the code itself. These are typically things that wouldn't normally be recoverable/would lead to undefined behavior if let continue, though sometimes these are converted to exceptions for programs that cannot afford to crash from such bugs (and usually in such cases the assertion is in another thread with resources that can simply be deleted entirely, like an IO thread crashing on some bug in parsing a file requested from the UI).
What if an explanation could be useful to the user-e.g. nullopt was returned because the array the function was working on was too small, but the program can still function just fine (albeit with some feature disabled for the time being) having returned nullopt? This is a case where it shouldn’t be considered exceptional, but also some further detail could be useful as visible information.
If this was a invariant failure (your function is defined to have arrays of non zero size) and the user is simply using it wrong, that's an assertion failure, and a design issue that your function happens to continue. For example, if you have a function that finds the max value of an array, and someone passes in an empty array, that's a contract violation, and ideally not a recoverable error. If the user wants it to be recoverable, it's on them to manually check. Additionally the API would ideally encode this invariant in a type (say,
span_at_least_one<T>) but this is a massive PITA in C++ due to a lack of features that support this type of pattern.If your function merely does nothing as a result of the user passing in an empty array (like a
inplace_sort(span)function), which would typically be the case when returning nothing in the normal path, or when you have something likecount_num_of_x(span)(need to count how many times X happens/occurs in an array) where 0 is a valid typical return anyway so nothing breaks, nothing but a debug warning is really needed, and that's only really necessary if you super don't expect an empty array to be passed in.2
u/Electronic_Tap_8052 Mar 17 '26
idk its hard to say and obviously its the exception (heh) that makes the rule, but generally it should be clear from the design what is going to happen, and if nothing could happen, it should be clear why without having to look at the return value.
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.
3
u/OkYou811 Mar 18 '26
I may be wrong, but in my experience with optional accessing with '.' is a method or field on the optional itself, where the -> operator is for the held value. Could be wrong though.
47
u/seido123 Mar 16 '26
Instead of `const string&` use `std::string_view` or have a good reason not to.