Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent of Rust's Result<T, E> type?

I like using std::experimental::optional in my C++ code, but the problem is value_or requires the default value to be of the same type as the optional's value.

This doesn't work very well when I want an optional that either contains an int or contains an error message.

I guess I could use a union struct that has a boolean to indicate if the value is there or it's an error, but it sure would be nice if C++ just had a Result<T, E> type like Rust.

Is there any such type? Why hasn't Boost implemented it?

Result is really much more useful than Option, and surely the people at Boost are aware of its existence. Maybe I'll go read the Rust implementation and then copy it to C++?

Ex:

// Function either returns a file descriptor for a listening socket or fails
// and returns a nullopt value.
// My issue: error messages are distributed via perror.
std::experimental::optional<int> get_tcp_listener(const char *ip_and_port);
// You can use value_or to handle error, but the error message isn't included!
// I have to write my own error logger that is contained within
// get_tcp_listener. I would really appreciate if it returned the error
// message on failure, rather than an error value.
int fd = get_tcp_listener("127.0.0.1:9123").value_or(-1);
// Rust has a type which does what I'm talking about:
let fd = match get_tcp_listener("127.0.0.1:9123") {
    Ok(fd) => fd,
    Err(msg) => { log_error(msg); return; },
}
like image 277
Peter Delevoryas Avatar asked Aug 20 '15 19:08

Peter Delevoryas


4 Answers

optional<T> is an asymmetric type safe union of T and nothingness (nullopt_t). You can query if it has a T with explicit operator bool, and get the T out with unary *. The asymmetry means that optional "prefers" to be a T, which is why unqualified operations (like * or operator bool) refer to its Tness.

variant<A,B,C> from paper n4218 is a symmetric type safe union of A, B and C (etc). boost::variant is always engaged, and std::experimental::variant is almost always engaged.

As it is symmetric, there is no unique type for unary * to return, and explicit operator bool cannot say much of interest, so neither are supported.

Instead, you have to visit it, or query it for particular types.

std::experimental::expected<E, T> from paper n4015 is an asymmetric type-safe union. It is either a T, or an E. But like optional, it "prefers" to be a T; it has an explicit operator bool that tells you if it is a T, and unary * gets the T.

In a sense, expected<E,T> is an optional<T>, but when empty instead of wasting the space it stores an E, which you can query.

Result<T,E> seems close to expected<E,T> (note that as of n4015, the order of parameters are swapped compared to Result).

like image 91
Yakk - Adam Nevraumont Avatar answered Nov 16 '22 14:11

Yakk - Adam Nevraumont


What you are looking for is exactly Alexandrescu's Expected. I recommend listening to his talk for an in depth understanding: https://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C. He actually goes through the implementation line by line, and you can easily write it yourself and use it well after that.

Variant is a more general tool, it can be coerced to do what you want but you're better off with expected.

like image 32
Nir Friedman Avatar answered Nov 16 '22 13:11

Nir Friedman


If not only boost is involved u can use result. This is nice single header container.

like image 7
S.R Avatar answered Nov 16 '22 14:11

S.R


optional by design either contains a value of some type or nothing.

You may be looking for something like Boost::Variant.

This is not yet part of the standard library, although something like it may be eventually.

like image 4
Alan Stokes Avatar answered Nov 16 '22 13:11

Alan Stokes