title: How to pass const pointer to const object using unique_ptr
body: <p>I want to pass a <code>unique_ptr</code> to a helper function, and I want to make sure that the helper function neither modifies the pointer, nor the pointed object. Without the <code>unique_ptr</code>, the solution is to have</p>
  
  <pre><code>void takePtr(AClass const * const aPtr) {
    // Do something with *aPtr. 
    // We cannot change aPtr, not *aPtr. 
  }
  </code></pre>
  
  <p>(Well, technically, <code>AClass const * aPtr</code> is enough.) And I can call this with </p>
  
  <pre><code>AClass * aPtr2 = new AClass(3);
  takePtr(aPtr2);
  </code></pre>
  
  <p>I want to instead use <code>unique_ptr</code>, but cannot figure out how to write this. I tried </p>
  
  <pre><code>void takeUniquePtr(unique_ptr&lt;AClass const&gt; const &amp; aPtr) {
    // Access *aPtr, but should not be able to change aPtr, or *aPtr. 
  }
  </code></pre>
  
  <p>When I call this with </p>
  
  <pre><code>unique_ptr&lt;AClass&gt; aPtr(new AClass(3));
  takeUniquePtr(aPtr);
  </code></pre>
  
  <p>it does not compile. The error I see is </p>
  
  <pre><code>testcpp/hello_world.cpp:141:21: error: invalid user-defined conversion from ‘std::unique_ptr&lt;AClass&gt;’ to ‘const std::unique_ptr&lt;const AClass&gt;&amp;’ [-fpermissive]
  </code></pre>
  
  <p>Shouldn't the conversion from <code>unique_ptr&lt;AClass&gt;</code> to <code>unique_ptr&lt;AClass const&gt;</code> be automatic? What am I missing here? </p>
  
  <p>By the way, if I change <code>unique_ptr&lt;AClass const&gt; const &amp; aPtr</code> to <code>unique_ptr&lt;AClass&gt; const &amp; aPtr</code> in the function definition, it compiles, but then I can call functions like <code>aPtr-&gt;changeAClass()</code>, which I don't want to allow. </p>