title: Using std::unique_ptr for Windows HANDLEs
body: <p>I am attempting to use std::unique_ptrs to manage Windows HANDLEs in an exception-safe manner.</p>
  
  <p>First I tried:</p>
  
  <pre><code>struct HandleDeleter
  {
      void operator()( HANDLE handle )
      {
          if( handle )
          {
              FindVolumeClose( handle )
          }
      }
  }
  typedef std::unique_ptr&lt; HANDLE, HandleDeleter &gt; unique_vol_handle_t;
  </code></pre>
  
  <p>Later in my code when I try to use it:</p>
  
  <p><code>unique_vol_handle_t volH( FindFirstVolumeW( buffer, MAX_GUID_PATH ) );</code></p>
  
  <p>I get the following error from Visual Studio 2012RC:</p>
  
  <pre><code>1&gt;          error C2664: 'std::unique_ptr&lt;_Ty,_Dx&gt;::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'HANDLE' to 'std::nullptr_t'
  1&gt;          with
  1&gt;          [
  1&gt;              _Ty=HANDLE,
  1&gt;              _Dx=VolumeHandleDeleter
  1&gt;          ]
  1&gt;          nullptr can only be converted to pointer or handle types
  </code></pre>
  
  <p>referencing the volH declaration line, immediately above.</p>
  
  <p>After searching for some time, I found <a href="http://codesequoia.wordpress.com/2012/08/26/stdunique_ptr-for-windows-handles/#comment-45" rel="noreferrer">a blog article</a> which basically says, add:</p>
  
  <p><code>typedef HANDLE pointer;</code></p>
  
  <p>to the top of the struct declaration, and all will be well.</p>
  
  <p>I didn't believe it, but I tried it and it did resolve the error.  I'm puzzled how defining a type (without even referencing it) could make such a difference.</p>
  
  <p>Two questions:</p>
  
  <p>1) Can you explain the original error?  I don't understand why the compiler is referring to <code>std::nullptr_t/nullptr</code>.</p>
  
  <p>2) How is it that the typedef resolves this (or at least appears to)?  Is there a less 'spooky action at a distance' solution to this?</p>