Note that there are some explanatory texts on larger screens.

plurals
  1. POmsvc 11 only respects C++ extern templates in certain situations
    primarykey
    data
    text
    <p>I'm messing around with C++ extern templates to speed up compilation. I've noticed that Visual Studio 2012 has made some improvements in its handling of <code>extern template</code>, but I'm still finding issues. Here's an example:</p> <p><strong>main.cpp:</strong></p> <pre><code>#include &lt;iostream&gt; #include "Calc.h" using namespace std; int main(int argc, char** argv) { Calc&lt;int&gt; c1; cout &lt;&lt; c1.add(1, 2) &lt;&lt; endl; } </code></pre> <p><strong>Calc.h:</strong></p> <pre><code>#pragma once template &lt;class A_Type&gt; class Calc { public: A_Type add(A_Type x, A_Type y) { return x + y; } }; extern template class Calc&lt;int&gt;; </code></pre> <p>Normally I'd include <strong>Calc.cpp</strong> with <code>template class Calc&lt;int&gt;;</code>, ensuring the template is only instantiated once. For this example, I am only compiling <strong>Main.cpp</strong> and expecting it to fail.</p> <p>The problem is, Visual Studio 2012 will happily compile and link the code above. It's not respecting the <code>extern template</code>. The only solution I've found is to do this in the header:</p> <p><strong>Calc.h:</strong></p> <pre><code>#pragma once template &lt;class A_Type&gt; class Calc { public: A_Type add(A_Type x, A_Type y); }; template &lt;class A_Type&gt; A_Type Calc&lt;A_Type&gt;::add(A_Type x, A_Type y) { return x + y; } extern template class Calc&lt;int&gt;; </code></pre> <p>Visual Studio fails to link if the header is like this (complaining about a missing definition for <code>Calc&lt;int&gt;::add</code>, as expected). <strong>However</strong>, <em>g++ 4.6.3</em> Fails to link either of these examples.</p> <p>Who is right? If both the prototypes and definitions of functions in a class are specified in the same header file, is there really a difference? Is there any way to specify the "shorter-form" <em>Calc.h</em> and have it fail as expected in Visual Studio 2012 (as well as gcc)?</p>
    singulars
    1. This table or related slice is empty.
    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