Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(<strong>Edited</strong>: This is slightly more robust version which allows the implementation to be compiled into a separate .o file. The template should be explicitly instantiated at the end of the implementation cpp file. Perhaps this was only an issue for g++.)</p> <p>You don't need to put the implementations in the header <em>if</em> you know which templates will be instantiated and can list them in the <strike>header</strike> implementation file. For example, if you know that you will only use <code>int</code> and <code>std::string</code>, then you can put this in the header file:</p> <pre><code>// test.h template &lt;class A&gt; class Test { public: void f(); }; </code></pre> <p>and put the implementation of f() into a normal test.cpp file:</p> <pre><code>// test.cpp #include "test.h" template &lt;class A&gt; void Test&lt;A&gt;::f() { // implementation } template class Test&lt;int&gt;; template class Test&lt;string&gt;; </code></pre> <p>Those last two lines explicitly instantiate the template classes. It is better to put this at the end of the implementation file, after it has seen the implementations of the member functions. Then you can compile it to a .o file <code>g++ -c test.cpp</code>. This test.o file will contain complete implementations of both <code>Test&lt;int&gt;</code> and <code>Test&lt;string&gt;</code> and can be linked without any difficulty in the rest of your application.</p> <p>It works, but is it a good idea? It depends on context. In many cases, this works very well. If you are writing a template for 'internal' use in your project, then you know which templates will be instantiated and which will not. But if instead you're making something available to the public which must be very flexible, then you <em>will</em> need to include the implementations in the header file.</p> <p>A tip: Even if it's for public consumption, take a look at the methods and see if there are any methods whose parameters and return type are independent of the template parameters. If so, you could place them into a Base class as (pure) virtual functions. This Base class doesn't use any templates and hence you may be able to use this <code>Base*</code> in much of your application (<code>template &lt;class A&gt; class Test : public Base { ...</code>, allowing you to limit the reach of the templated code throughout your application. I found this useful recently when much of the underlying behaviour and construction of a class depended on the template parameters, but the <em>interface</em> to the already-constructed object did not depend on the template parameters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload