title: C++ equivalent of Rust's Result<T, E> type?
body: <p>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.</p>
  
  <p>This doesn't work very well when I want an optional that either contains an int or contains an error message.</p>
  
  <p>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 <code>Result&lt;T, E&gt;</code> type like Rust.</p>
  
  <p>Is there any such type? Why hasn't Boost implemented it?</p>
  
  <p>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++?</p>
  
  <p>Ex:</p>
  
  <pre><code>// 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&lt;int&gt; 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) =&gt; fd,
      Err(msg) =&gt; { log_error(msg); return; },
  }
  </code></pre>