title: Best alternative to std::optional to return an optional value from a method? (using C++98/C++11/C++14)
body: <p>Obviously, <code>std::optional</code> is the best choice to return an optional value from a function if one uses C++17 or boost (see also <a href="http://herbsutter.com/2013/05/30/gotw-90-solution-factories/" rel="noreferrer">GOTW #90</a>)</p>
  
  <pre><code>std::optional&lt;double&gt; possiblyFailingCalculation()
  </code></pre>
  
  <p>But what and why would be the best alternative if one is stuck with an older version (and can't use boost)?</p>
  
  <p>I see a few options:</p>
  
  <ol>
  <li><p><strong>STL smart pointers</strong> (C++11 only)</p>
  
  <pre><code>std::unique_ptr&lt;double&gt; possiblyFailingCalculation();
  </code></pre>
  
  <ul>
  <li>(+) virtually the same usage as optional</li>
  <li>(&minus;) confusing to have smart pointers to non-polymorphic types or built-in types</li>
  </ul></li>
  <li><p><strong>Pairing it up with a bool</strong></p>
  
  <pre><code>std::pair&lt;double,bool&gt; possiblyFailingCalculation();
  </code></pre></li>
  <li><p><strong>Old style</strong></p>
  
  <pre><code>bool possiblyFailingCalculation(double&amp; output);
  </code></pre>
  
  <ul>
  <li>(&minus;) incompatible with new C++11   <code>auto value = calculation()</code>  style</li>
  </ul></li>
  <li><p><strong>A DIY template</strong>: a basic template with the same functionality is easy enough to code, but are there any pitfalls to implement a robust <code>std::optional&lt;T&gt;</code> look-a-like template ?</p></li>
  <li><p><strong>Throw an exception</strong></p>
  
  <ul>
  <li>(&minus;) Sometimes "impossible to calculate" is a valid return value.</li>
  </ul></li>
  </ol>