The std::expected is a C++23 type representing either a correct result or an unexpected error.
Semantically, std::expected works very similarly to std::optional; instead of optionally storing a value, std::expected always stores either a result or an error.
Compiler Explorer link: https://compiler-explorer.com/z/3eaEW7Yzr
@simontoth do you know why expected<T&,E> is not supported, like std::optional<T&>. the latter was a mistake that I still want to fix
@PeterSommerlad No idea, but personally, I'm not comfortable with returning wrapped references.
The moment we get compilers to detect pass-through references, then I will be fine with it, but right now, it's just way too easy to trip over and it can be hard for sanitizers to notice.
@simontoth
with optional<T&> as a return type you can implement safe wrappers around containers when exceptions are not suitable. similar with parameters that need to be references.
I sometimes used optional<reference_wrapper<T>> but it falls short in generic code requiring .value().get()
because implicit conversion of reference_wrapper is not triggered.
Using plain pointers for those cases as a convention as the core guidelines suggest
is an abomination.