Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First you must understand templates mechanism. Templates are not compiled, they are instantiated when they are used and then their instantiation is compiled. So the compiler needs to have the full template definition in each module using the template function, in order to instantiate them first according to the parameters you've passed.</p> <p>To solve your problem, there are three solutions but you'll see that they both lead to the same result. Either you implement your whole templates in your header file inside the class definition (we use to suffix them with .hxx instead of .h in order to precise they're containing templates definitions):</p> <pre><code>// Foo.hxx #ifndef __FOO_HXX__ #define __FOO_HXX__ class Foo { public: template &lt;class T&gt; void bar(const T&amp; t) { t.doSomething(); } }; #endif </code></pre> <p>Or you can externalize the definition from the class, but still in the header file:</p> <pre><code>// Foo.hxx #ifndef __FOO_HXX__ #define __FOO_HXX__ class Foo { public: template &lt;class T&gt; void bar(const T&amp;); }; template &lt;class T&gt; void Foo::bar(const T&amp; t) { t.doSomething(); } #endif </code></pre> <p>Finally, you can implement template methods bodies in an external file (prefixed with .cxx for the same reason). It will contain methods' bodies but won't include "Foo.hxx". Instead, it's "Foo.hxx" that will include "Foo.cxx" after the class definition. This way, when the compiler resolves the #include directive, it finds the whole template definition in the same module, allowing it to instantiate it:</p> <pre><code>// Foo.hxx #ifndef __FOO_HXX__ #define __FOO_HXX__ class Foo { public: template &lt;class T&gt; void bar(const T&amp;); }; #include "Foo.cxx" #endif // Foo.cxx template &lt;class T&gt; void Foo::bar(const T&amp; t) { t.doSomething(); } </code></pre> <p>The choice between these 3 ways to implement templates is rather a matter of readability (and taste).<br> Second and third are equivalent in terms of generated code, but I'd rather not use the cxx file solution, because it often leads to stupid errors when you forget to invert the include. </p> <p>Moreover, well-known C++ libraries like STL or Boost propose their code in header files only, which is a sign of good design. By using external definition inside headers, you clarify the definition of your class. You also prevent the compiler to automatically inline methods, which can sometimes lead to poor results according to Herb Sutter <a href="http://www.gotw.ca/gotw/033.htm">http://www.gotw.ca/gotw/033.htm</a></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