Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The compiler <strong><em>generates</em></strong> the code for the specific types given in the template class instantiation.</p> <p>If you have for instance a template class declaration as</p> <pre><code>template&lt;typename T&gt; class Foo { public: T&amp; bar() { return subject; } private: T subject; }; </code></pre> <p>as soon you have for example the following instantiations</p> <pre><code>Foo&lt;int&gt; fooInt; Foo&lt;double&gt; fooDouble; </code></pre> <p>these will <strong><em>effectively generate</em></strong> the same linkable code as you would have defined classes like</p> <pre><code>class FooInt { public: int&amp; bar() { return subject; } private: int subject; } </code></pre> <p>and </p> <pre><code>class FooDouble { public: double&amp; bar() { return subject; } private: double subject; } </code></pre> <p>and instantiate the variables like</p> <pre><code>FooInt fooInt; FooDouble fooDouble; </code></pre> <p>Regarding the point that template <a href="https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration"><strong>definitions</strong></a> (don't confuse with <a href="https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration">declarations</a> regardless of templates) need to be seen with the header (included) files, it's pretty clear why:<br> The compiler can't generate this code without seeing the <a href="https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration"><strong>definition</strong></a>. It can refer to a matching instantiation that appeared first at linking stage though.</p> <p><strong>UPDATE:</strong> </p> <blockquote> <p>What does a non-template member function have that allows for it to be defined outside of the header that a template function doesn't have?</p> </blockquote> <p>The declaration of a non-template class/member/function gives a predefined entry point for the linker. The definition can be drawn from a single implementation seen in a compiled object file (== .cpp == <a href="https://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c">compilation unit</a>).<br> In contrast the declaration of a templated class/member/function might be instantiated from arbitrary compilation units given the same or varying template parameters. The definition for these template parameters need's to be seen at least once. It can be either generic or specialized.</p> <p>Note that you can specialize template implementations for particular types anyway (included with the header or at a specific <a href="https://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c">compilation unit</a>). If you would provide a specialization for your template class in one of your <a href="https://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c">compilation units</a>, and don't use your template class with types other than specialized, that also should suffice for linking it all together.</p> <p>I hope this sample helps clarifying what's the difference and efforts done from the compiler.</p>
 

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