Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically separate class definitions from declarations?
    primarykey
    data
    text
    <p>I am using a library that consists almost entirely of <strong>templated classes and functions in header files</strong>, like this:</p> <pre><code>// foo.h template&lt;class T&gt; class Foo { Foo(){} void computeXYZ() { /* heavy code */ } }; template&lt;class T&gt; void processFoo(const Foo&lt;T&gt;&amp; foo) { /* more heavy code */ } </code></pre> <p>Now this is bad because <strong>compile times are unbearable</strong> whenever I include one of those header files (and actually I include many of them in each of my compilation units).</p> <p>Since as a template parameter I only use one or two types anyway I am planning to create, for each library header file, <strong>a file that contains only declarations</strong>, without the heavy code, like this:</p> <pre><code>// NEW: fwd-foo.h template&lt;class T&gt; class Foo { Foo(); void computeXYZ(); }; template&lt;class T&gt; void processFoo(const Foo&lt;T&gt;&amp; foo); </code></pre> <p>And then one file that creates all the instantiations that I'll need. That file can be <strong>compiled separately once and for all</strong>:</p> <pre><code>// NEW: foo.cpp #include "foo.h" template class Foo&lt;int&gt;; template class Foo&lt;double&gt;; template void processFoo(const Foo&lt;int&gt;&amp; foo); template void processFoo(const Foo&lt;double&gt;&amp; foo); </code></pre> <p>Now I can just include <code>fwd-foo.h</code> in my code and have short compile times. I'll link against <code>foo.o</code> at the end. </p> <p>The downside, of course, is that I have to create these new <code>fwd-foo.h</code> and <code>foo.cpp</code> files myself. And of course it's a maintenance problem: When a new library version is released I have to adapt them to that new version. Are there any other downsides?</p> <p>And my main question is: </p> <p>Is there any chance I can create these new files, especially <code>fwd-foo.h</code>, <strong>automatically</strong> from the original <code>foo.h</code>? I have to do this for many library header files (maybe 20 or so), and an automatic solution would be best especially in case a new library version is released and I have to do this again with the new version. Are any tools available for this task?</p> <p><strong>EDIT:</strong> </p> <p>Additional question: How can the newly supported <code>extern</code> keyword help me in this case?</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.
 

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