title: Am I abusing std::optional
body: <h2>How <code>std::optional</code> fits in my code?</h2>
  
  <p>My code contains MANY functions that looks roughly like:</p>
  
  <pre><code>bool GetName(_Out_ string&amp; strRes)
  {
     // try to get/calculate the value
     // value may also be empty
     // return true on success, false otherwise.
  }
  </code></pre>
  
  <p>Now that I have found out <a href="https://en.cppreference.com/w/cpp/utility/optional" rel="nofollow noreferrer">std::optional</a>, I simply code:</p>
  
  <pre><code>std::optional&lt;std::string&gt; GetName()
  </code></pre>
  
  <h2>Why or when not to use <code>std::optional</code>?</h2>
  
  <ol>
  <li>I understand that using <code>std::optional</code> comes with a performance price and for the sake of argument, let's assume I'm willing to pay it.</li>
  <li>I also understand that coding something like:
  <code>std::optional&lt;int&gt; iVal = 9;</code> is pointless.</li>
  </ol>
  
  <h2>Am I overly charmed by <code>std::optional</code>?</h2>
  
  <p>I find <code>std::optional</code> so natural that it lead me to a conclusion that, <strong>by default</strong>, many of my native types, <code>bool</code>, <code>int</code>, <code>string</code>, should be declared as <code>std::optional</code>.</p>
  
  <p>So, instead of following the premise of:</p>
  
  <blockquote>
    <p>Use <code>std::optional</code> <strong>only</strong> when absolutely needed</p>
  </blockquote>
  
  <p>I'm leaning towards:</p>
  
  <blockquote>
    <p>Use <code>std::optional</code> always <strong>unless</strong> you are sure it isn't needed now or
    in the future.</p>
  </blockquote>
  
  <h2>Question 1</h2>
  
  <p>Is the following rule valid:</p>
  
  <p>use <code>std::optional</code> when:</p>
  
  <ol>
  <li>a value is calculated on run time</li>
  <li>calculation may fail</li>
  <li>you want to know if calculation failed</li>
  </ol>
  
  <h2>Question 2</h2>
  
  <p>If using <code>std::optional</code> becomes abundant, am I risking code readability issues?</p>
  
  <hr>
  
  <h2> Update</h2>
  
  <p>Answers and comments steered the discussion a bit away from my initial concern which is an attempt to define a rule of thumb for when to use <code>std::optional</code>. </p>
  
  <p>I will address some of the common feedback I got:</p>
  
  <blockquote>
    <p>Your are coding C-style error checking functions.</p>
    
    <p>use exceptions when the calculation fails.</p>
  </blockquote>
  
  <p>It is perhaps my fault for providing a not-so-good example of my motivation. <code>GetName()</code> is just a taste of my common use case.
  Instead of focusing on how errors are reflected (if any), I wanted to focus on <strong>is name a good candidate for optional?</strong></p>
  
  <p>I didn't state that if <code>GetName</code> returns false it implies some sort of error in my flow. When <code>GetName</code> return false, I could either generate a random name or just ignore the current iteration. Regardless of how the callers reacts to the return value, <code>name</code> is <code>optional</code> in a sense that it <code>may not be present</code>.</p>
  
  <p>I can provide a better example: </p>
  
  <pre><code>void GetValuesFromDB(vector&lt;string&gt; vecKeys, map&lt;string,string&gt; mapKeyValue);
  </code></pre>
  
  <p>in this function I'm "forced" to provide two containers since I want to distinguish between:</p>
  
  <ol>
  <li>key was found in DB and it is empty. this key will be in <code>mapKeyValue</code></li>
  <li>key was not found in DB. . this key will NOT be in <code>mapKeyValue</code></li>
  </ol>
  
  <p>But with <code>optional</code> I could go:</p>
  
  <pre><code>void GetValuesFromDB(map&lt;string,std::optional&lt;string&gt; &gt; mapKeyValue);
  </code></pre>
  
  <hr>
  
  <p>I believe that we're taking the word <code>optional</code> too literally.</p>
  
  <p>I really support the notion that one may use <code>std::optional</code> <a href="https://www.bfilipek.com/2018/05/using-optional.html" rel="nofollow noreferrer">If you want to represent a nullable type nicely. Rather than using unique values (like -1, nullptr, NO_VALUE or something)</a> </p>
  
  <p>The c++ standard committee could have easily decided to name <code>std::optional</code>, <code>std::nullable</code>. </p>