title: When does unique_ptr require a complete type?
body: <p>In the code below function <code>f()</code> can call the <code>operator bool()</code> and <code>operator *()</code> member functions of <code>unique_ptr&lt;C&gt;</code> for the incomplete <code>class C</code>. However when function <code>g()</code> tries to call those same member functions for <code>unique_ptr&lt;X&lt;C&gt;&gt;</code>, the compiler suddenly wants a complete type and tries to instantiate <code>X&lt;C&gt;</code>, which then fails. For some reason <code>unique_ptr&lt;X&lt;C&gt;&gt;::get()</code> does not cause template instantiation and compiles correctly as can be seen in function <code>h()</code>. Why is that? What makes <code>get()</code> different from <code>operator bool()</code> and <code>operator *()</code>?</p>
  
  <pre><code>#include &lt;memory&gt;
  
  class C;
  std::unique_ptr&lt;C&gt; pC;
  
  C&amp; f() {
      if ( !pC ) throw 0; // OK, even though C is incomplete
      return *pC;         // OK, even though C is incomplete
  }
  
  template &lt;class T&gt;
  class X
  {
      T t;
  };
  
  std::unique_ptr&lt;X&lt;C&gt;&gt; pX;
  
  X&lt;C&gt;&amp; g() {
      if ( !pX ) throw 0; // Error: 'X&lt;C&gt;::t' uses undefined class 'C'
      return *pX;         // Error: 'X&lt;C&gt;::t' uses undefined class 'C'
  }
  
  X&lt;C&gt;&amp; h() {
      if ( !pX.get() ) throw 0; // OK
      return *pX.get();         // OK
  }
  
  class C {};
  </code></pre>