Simon Brand (a.k.a. TartanLlama) recently published an article called “Functional exceptionless error-handling with optional and expected” where he excellently explains how sum types (e.g. std::optional
and std::variant
) can be used in lieu of exceptions in order to implement error handling.
The article focuses on the usage of std::optional
, on std::expected
, and on two monadic operations, map
and and_then
, which reduce boilerplate and increase readability of the code. While Simon briefly mentions the benefits of ADTs (algebraic data types) over exceptions, I think that the topic deserves more attention, as some readers missed the point.
Here’s an example comment from the article’s /r/cpp thread: