title: Preventing header explosion in C++ (or C++0x)
body: <p>Lets say with have generic code like the following:</p>
  
  <p><strong><code>y.hpp:</code></strong></p>
  
  <pre><code>#ifndef Y_HPP
  #define Y_HPP
  
  // LOTS OF FILES INCLUDED
  
  template &lt;class T&gt;
  class Y 
  {
  public:
    T z;
    // LOTS OF STUFF HERE
  };
  
  #endif
  </code></pre>
  
  <p>Now, we want to be able to use a Y in a class (say X) we create. However, we don't want users of X to have to include the Y headers.</p>
  
  <p>So we define a class X, something like this:</p>
  
  <p><strong><code>x.hpp:</code></strong></p>
  
  <pre><code>#ifndef X_HPP
  #define X_HPP
  
  template &lt;class T&gt;
  class Y;
  
  class X
  {
  public:
    ~X();
    void some_method(int blah);
  private:
    Y&lt;int&gt;* y_;
  };
  
  #endif
  </code></pre>
  
  <p>Note that, because y_ is a pointer, we don't need to include its implementation.</p>
  
  <p>The implementation is in x.cpp, which is separately compiled:</p>
  
  <p><strong><code>x.cpp:</code></strong></p>
  
  <pre><code>#include "x.hpp"
  #include "y.hpp"
  
  X::~X() { delete y_; }
  void X::someMethod(int blah) { y_-&gt;z = blah; }
  </code></pre>
  
  <p>So now our clients can just include "x.hpp" to use X, without including and having to process all of "y.hpp" headers:</p>
  
  <p><strong><code>main.cpp:</code></strong></p>
  
  <pre><code>#include "x.hpp"
  
  int main() 
  {
    X x;
    x.blah(42);
    return 0; 
  }
  </code></pre>
  
  <p>And now we can compile <code>main.cpp</code> and <code>x.cpp</code> separately, and when compiling <code>main.cpp</code> I don't need to include <code>y.hpp</code>.</p>
  
  <p>However with this code I've had to use a raw pointer, and furthermore, I've had to use a delete.</p>
  
  <p>So here are my questions:</p>
  
  <p><strong>(1)</strong> Is there a way I could make Y a direct member (not a pointer to Y) of X, without needing to include the Y headers? <em>(I strongly suspect the answer to this question is no)</em></p>
  
  <p><strong>(2)</strong> Is there a way I could use a smart pointer class to handle the heap allocated Y? <code>unique_ptr</code> seems like the obvious choice, but when I change the line in <code>x.hpp</code></p>
  
  <p>from:</p>
  
  <pre><code>Y&lt;int&gt;* y_; 
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>std::unique_ptr&lt; Y&lt;int&gt; &gt; y_;
  </code></pre>
  
  <p>and include , and compile with c++0x mode, I get the error:</p>
  
  <pre><code>/usr/include/c++/4.4/bits/unique_ptr.h:64: error: invalid application of ‘sizeof’ to incomplete type ‘Y&lt;int&gt;’ 
  /usr/include/c++/4.4/bits/unique_ptr.h:62: error: static assertion failed: "can't delete pointer to incomplete type"
  </code></pre>
  
  <p>so is there anyway to do this by using a standard smart pointer instead of a raw pointer and also a raw delete in a custom destructor?</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>Howard Hinnant has got it right, all we need to do is change <code>x.hpp</code> and <code>x.cpp</code> in the following fashion:</p>
  
  <p><strong><code>x.hpp:</code></strong></p>
  
  <pre><code>#ifndef X_HPP
  #define X_HPP
  
  #include &lt;memory&gt;
  
  template &lt;class T&gt;
  class Y;
  
  class X
  {
  public:
    X(); // ADD CONSTRUCTOR FOR X();
    ~X();
    void some_method(int blah);
  private:
    std::unique_ptr&lt; Y&lt;int&gt; &gt; y_;
  };
  
  #endif
  </code></pre>
  
  <p><strong><code>x.cpp:</code></strong></p>
  
  <pre><code>#include "x.hpp"
  #include "y.hpp"
  
  X::X() : y_(new Y&lt;int&gt;()) {} // ADD CONSTRUCTOR FOR X();
  X::~X() {}
  void X::someMethod(int blah) { y_-&gt;z = blah; }
  </code></pre>
  
  <p>And we're good to use unique_ptr. Thanks Howard!</p>
  
  <p><strong>Rationale behind solution:</strong></p>
  
  <p>People can correct me if I'm wrong, but the issue with this code was that the implicit default constructor was trying to default initialize Y, and because it doesn't know anything about Y, it can't do that. By explicitly saying we will define a constructor elsewhere, the compiler thinks "well, I don't have to worry about constructing Y, because it's compiled elsewhere". </p>
  
  <p>Really, I should have added a constructor in the first place, my program is buggy without it.</p>