Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
Prev Up HomeNext

std expected

std::expected<T, E> came originally from an experimental monadic and generic programming library outside of Boost written by Boost and WG21 developers around 2013. Before Outcome v1, I deployed the then Expected into a large codebase and I was dismayed with the results, especially on build times. You can read here how those experiences led me to develop Outcome v1.

std::expected<T, E> is a constrained variant type with a strong preference for the successful type T which it models like a std::optional<T>. If, however, there is no T value then it supplies an ‘unexpected’ E value instead. std::expected<T, E> was standardised in the C++ 23 standard.

Outcome’s Result type can be configured to act just like Expected if you want that, however ultimately Outcome’s Result doesn’t solve the same problem as Expected, plus Outcome models std::variant<T, E> rather than std::optional<T> which we think much superior for many use cases, which to summarise:

Outcome recognises Expected-like types and will construct from them, which aids interoperability.

Pros:

Cons:

Last revised: January 10, 2022 at 14:29:13 UTC


Prev Up HomeNext