title: How does std::unique_ptr work when it goes out of scope after the object it owns has been manually deleted?
body: <p>I may not have worded the title correctly, but it is easier to explain in the following example, and then perhaps someone can edit the title.</p>
  
  <p>Consider the following snippet of code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt; // for std::unique_ptr
  
  class Resource
  {
  public:
      Resource() { std::cout &lt;&lt; "Resource acquired\n"; }
      ~Resource() { std::cout &lt;&lt; "Resource destroyed\n"; }
  };
  
  int main()
  {
      Resource* res = new Resource;
      std::unique_ptr&lt;Resource&gt; res1(res); // Resource created here
      delete res;
  
      std::cout &lt;&lt; "res1 is " &lt;&lt; (static_cast&lt;bool&gt;(res1) ? "not null\n" : "null\n");
  
      return 0;
  }
  </code></pre>
  
  <p>This prints:</p>
  
  <pre><code>$ ./a.out 
  Resource acquired
  Resource destroyed
  res1 is not null
  Resource destroyed
  </code></pre>
  
  <p>We created a dynamically allocated resource, and then created a unique pointer (res1) who owns this resource. The unique pointer allows us to not worry about having to manually delete the resource.</p>
  
  <p>But let's suppose we manually delete the resource anyway (without setting it to null after) as in the above code. Then when res1 goes out of scope, wouldn't it be trying to delete something that has already been deallocated?</p>