title: Where to put a member function template
body: <p>An aspect of C++ that periodically frustrates me is deciding where templates fit between header files (traditionally describing the interface) and implemention (.cpp) files. Templates often need to go in the header, exposing the implementation and sometimes pulling in extra headers which previously only needed to be included in the .cpp file. I encountered this problem yet again recently, and a simplified example of it is shown below.</p>
  
  <pre><code>#include &lt;iostream&gt; // for ~Counter() and countAndPrint()
  
  class Counter
  {
    unsigned int count_;
  public:
    Counter() : count_(0) {}
    virtual ~Counter();
  
    template&lt;class T&gt;
    void
    countAndPrint(const T&amp;a);
  };
  
  Counter::~Counter() {
      std::cout &lt;&lt; "total count=" &lt;&lt; count_ &lt;&lt; "\n";
  }
  
  template&lt;class T&gt;
  void
  Counter::countAndPrint(const T&amp;a) {
    ++count_;
    std::cout &lt;&lt; "counted: "&lt;&lt; a &lt;&lt; "\n";
  }
  
  // Simple example class to use with Counter::countAndPrint
  class IntPair {
    int a_;
    int b_;
  public:
    IntPair(int a, int b) : a_(a), b_(b) {}
    friend std::ostream &amp;
    operator&lt;&lt;(std::ostream &amp;o, const IntPair &amp;ip) {
      return o &lt;&lt; "(" &lt;&lt; ip.a_ &lt;&lt; "," &lt;&lt; ip.b_ &lt;&lt; ")";
    }
  };
  
  int main() {
    Counter ex;
    int i = 5;
    ex.countAndPrint(i);
    double d=3.2;
    ex.countAndPrint(d);
    IntPair ip(2,4);
    ex.countAndPrint(ip);
  }
  </code></pre>
  
  <p>Note that I intend to use my actual class as a base class, hence the virtual destructor; I doubt it matters, but I've left it in Counter just in case. The resulting output from the above is</p>
  
  <pre><code>counted: 5
  counted: 3.2
  counted: (2,4)
  total count=3
  </code></pre>
  
  <p>Now <code>Counter</code>'s class declaration could all go in a header file (e.g., counter.h).  I can put the implementation of the dtor, which requires iostream, into counter.cpp. But what to do for the member function template <code>countAndPrint()</code>, which also uses iostream? It's no use in counter.cpp since it needs to be instantiated outside of the compiled counter.o. But putting it in counter.h means that anything including counter.h also in turn includes iostream, which just seems wrong (and I accept that I may just have to get over this aversion). I could also put the template code into a separate file (counter.t?), but that would be a bit surprising to other users of the code. <a href="https://rads.stackoverflow.com/amzn/click/com/0201633620" rel="nofollow noreferrer" rel="nofollow noreferrer">Lakos</a> doesn't really go into this as much as I'd like, and the <a href="http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13" rel="nofollow noreferrer">C++ FAQ</a> doesn't go into best practice. So what I'm after is:</p>
  
  <ol>
  <li>are there any alternatives for dividing the code to those I've suggested?</li>
  <li>in practice, what works best?</li>
  </ol>