Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would you use Alexandrescu's Expected<T> with void functions?
    text
    copied!<p>So I ran across this (IMHO) very nice idea of using a composite structure of a return value and an exception - <code>Expected&lt;T&gt;</code>. It overcomes many shortcomings of the traditional methods of error handling (exceptions, error codes).</p> <p>See the <a href="http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C" rel="noreferrer">Andrei Alexandrescu's talk (Systematic Error Handling in C++)</a> and <a href="https://skydrive.live.com/?cid=f1b8ff18a2aec5c5&amp;id=F1B8FF18A2AEC5C5!1158" rel="noreferrer">its slides</a>.</p> <p>The exceptions and error codes have basically the same usage scenarios with functions that return something and the ones that don't. <code>Expected&lt;T&gt;</code>, on the other hand, seems to be targeted only at functions that return values.</p> <p>So, my questions are:</p> <ul> <li>Have any of you tried <code>Expected&lt;T&gt;</code> in practice?</li> <li>How would you apply this idiom to functions returning nothing (that is, void functions)?</li> </ul> <p><strong>Update:</strong></p> <p>I guess I should clarify my question. The <code>Expected&lt;void&gt;</code> specialization makes sense, but I'm more interested in how it would be used - the consistent usage idiom. The implementation itself is secondary (and easy).</p> <p>For example, Alexandrescu gives this example (a bit edited):</p> <pre><code>string s = readline(); auto x = parseInt(s).get(); // throw on error auto y = parseInt(s); // won’t throw if (!y.valid()) { // ... } </code></pre> <p>This code is "clean" in a way that it just flows naturally. We need the value - we get it. However, with <code>expected&lt;void&gt;</code> one would have to capture the returned variable and perform some operation on it (like <code>.throwIfError()</code> or something), which is not as elegant. And obviously, <code>.get()</code> doesn't make sense with void.</p> <p>So, what would your code look like if you had another function, say <code>toUpper(s)</code>, which modifies the string in-place and has no return value?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload