title: How to return a const reference which is not available
body: <p>Say I have the following function:</p>
  
  <pre><code>const std::string&amp; Cat::getKittenName() const
  {
    Kitten* kitty = getKitty();
    return kitty-&gt;getName();
  }
  </code></pre>
  
  <p>Where <code>Kitten::getName</code> returns a <code>const std::string&amp;</code> how do I best handle the case where <code>kitty</code> is a <code>nullptr</code>? I could return <code>std::string("")</code> but then I am returning a reference to a temporary and practically guaranteeing undefined behaviour. I could change the <code>getKittenName</code> function to return a <code>std::string</code> to get around this but then I am introducing a redundant copy for all the cases where <code>kitty</code> is available. Right now I feel the best option is:</p>
  
  <pre><code>const std::string&amp; Cat::getKittenName() const
  {
    Kitten* kitty = getKitty();
    if (kitty)
    {
      return kitty-&gt;getName();
    }
    static std::string empty("");
    return empty;
  }
  </code></pre>
  
  <p>The only issue might be if 'magic statics' aren't available. Is there any problem with this solution or is there a better way to do it?</p>