title: C++ std::unique_ptr return from function and test for null
body: <p>I have a function that needs to return a pointer to an object of class <code>myClass</code>. For this purpose I´m using <code>std::unique_ptr</code>.</p>
  
  <p>If the function succeeds, it shall return a pointer to a object with data. If it fails, it should return <code>null</code>.</p>
  
  <p>This is my code skeleton:</p>
  
  <pre><code>std::unique_ptr&lt;myClass&gt; getData()
  {
     if (dataExists)
        ... create a new myClass object, populate and return it ...
  
     // No data found
     return std::unique_ptr&lt;myClass&gt; (null); // &lt;--- Possible?
  }
  </code></pre>
  
  <p>on <code>main</code>:</p>
  
  <pre><code>main()
  {
     std::unique_ptr&lt;myClass&gt; returnedData;
  
     returnedData = getData();
  
     if (returnedData != null)  // &lt;-- How to test for null?
     {
        cout &lt;&lt; "No data returned." &lt;&lt; endl;
        return 0;
     }
  
     // Process data
  }
  </code></pre>
  
  <p>So here goes my questions:</p>
  
  <p>a) Is that (returning an object or <code>null</code>) possible to be done using <code>std::unique_ptr</code>?</p>
  
  <p>b) If possible, how to implement is?</p>
  
  <p>c) If not possible, what are there alternatives?</p>
  
  <p>Thanks for helping.</p>