title: Returning a "NULL reference" in C++?
body: <p>In dynamically typed languages like JavaScript or PHP, I often do functions such as:</p>
  
  <pre><code>function getSomething(name) {
      if (content_[name]) return content_[name];
      return null; // doesn't exist
  }
  </code></pre>
  
  <p>I return an object if it exists or <code>null</code> if not.</p>
  
  <p>What would be the equivalent in C++ using references? Is there any recommended pattern in general? I saw some frameworks having an <code>isNull()</code> method for this purpose:</p>
  
  <pre><code>SomeResource SomeClass::getSomething(std::string name) {
      if (content_.find(name) != content_.end()) return content_[name];
      SomeResource output; // Create a "null" resource
      return output;
  }
  </code></pre>
  
  <p>Then the caller would check the resource that way:</p>
  
  <pre><code>SomeResource r = obj.getSomething("something");
  if (!r.isNull()) {
      // OK
  } else {
      // NOT OK
  }
  </code></pre>
  
  <p>However, having to implement this kind of magic method for each class seems heavy. Also it doesn't seem obvious when the internal state of the object should be set from "null" to "not null".</p>
  
  <p>Is there any alternative to this pattern? I  already know it can be done using pointers, but I am wondering how/if it can be done with references. Or should I give up on returning "null" objects in C++ and use some C++-specific pattern? Any suggestion on the proper way to do that would be appreciated.</p>