Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><i>these methods will have to be defined in the header file</i></p> <p>They have to be defined in <em>a</em> header file, so that if they're used then they're available in the translation unit where the template function is instantiated. If you're worried about too many templates in header files, slowing down compilation of translation units which use Mesh but don't actually use that template function, then you could move the implementation into a separate header file. Makes life slightly more complicated for clients, deciding whether to include the "full fat" class header or not, but it's not actually difficult.</p> <p>Alternatively, for this particular example you could define an output iterator for Mesh, which appends Normals. Then clients with their arbitrary iterators can do:</p> <pre><code>std::copy(first, last, mymesh.normalAdder()); </code></pre> <p>The only header they need with template code in it is <code>&lt;algorithm&gt;</code>, which they quite possibly have already. </p> <p>To do it yourself, the object returned by <code>normalAdder()</code> needs to overload <code>operator++()</code> and <code>operator*()</code>, which itself needs to return a proxy object (usually <code>*this</code>) which implements <code>operator=(const &amp;Vector3)</code>. That appends to the vector of normals. But all that is non-template code, and can be implemented in your .cpp file.</p> <p>Again in this example, <code>normalAdder()</code> could just return <code>std::back_inserter(this.normals);</code>, a template from <code>&lt;iterator&gt;</code>.</p> <p>As to whether you need to worry about it - I think when compilation times go skyward, it's more frequently due to unnecessary dependencies rather than due to small bits of template code in headers. Some large projects seem to need drastic measures, but personally I haven't worked with anything over about 100 files or so.</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