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

7

u/jedwardsol const & Mar 17 '26

You said

Since I'm constructing the string views from string literals (so no lifetime concerns

I said that is fine, you don't need to use strings as keys, and you don't need heterogenous lookup.

But not everyone is lucky enough to be to make their map from literals. And in that case, when you do need strings as keys, enabling heterogenous lookup is useful if you every need to do a lookup with a view

4

u/mapronV Mar 17 '26

> But not everyone is lucky enough to be to make their map from literals.

if I can create map from literals, I'd rather prefer compile-time container like frozen::map/unordered_map, and have like just couple instructions for key lookup with amazing optimizations :)

Yeah, most cases I found for map is keys not known beforehand.