<p>Suppose I have <code>fileA.h</code> which declares a class <code>classA</code> with template function <code>SomeFunc&lt;T&gt;()</code>. This function is implemented directly in the header file (as is usual for template functions). Now I add a specialized implementation of <code>SomeFunc()</code> (like for <code>SomeFunc&lt;int&gt;()</code>) in <code>fileA.C</code> (ie. not in the header file).</p>

<p>If I now call <code>SomeFunc&lt;int&gt;()</code> from some other code (maybe also from another library), would it call the generic version, or the specialization?</p>

<p>I have this problem right now, where the class and function live in a library which is used by two applications. And one application correctly uses the specialization, while another app uses the generic form (which causes runtime problems later on). Why the difference? Could this be related to linker options etc? This is on Linux, with g++ 4.1.2.</p>

[{'answer_id': 59359, 'author': 'Brandon', 'author_id': 5959, 'author_profile': 'https://Stackoverflow.com/users/5959', 'pm_score': 0, 'selected': False, 'text': '<p>Unless the specialized template function is also listed in the header file, the other application will have no knowledge of the specialized version.  The solution is the add <code>SomeFunc&lt;int&gt;()</code> to the header as well.</p>\n'}, {'answer_id': 59361, 'author': 'Serge', 'author_id': 1007, 'author_profile': 'https://Stackoverflow.com/users/1007', 'pm_score': 3, 'selected': False, 'text': "<p>Have you added a prototype with parameters to your header file?</p>\n\n<p>I mean is there somewhere in fileA.h</p>\n\n<pre><code>template&lt;&gt; SomeFunc&lt;int&gt;();\n</code></pre>\n\n<p>If not that's probably the reason.</p>\n"}, {'answer_id': 59368, 'author': 'oliver', 'author_id': 2148773, 'author_profile': 'https://Stackoverflow.com/users/2148773', 'pm_score': 0, 'selected': False, 'text': "<p>Brandon: that's what I thought - the specialized function should never be called. Which is true for the second application I mentioned. The first app, however, clearly calls the specialized form even though the specialization is not declared in the header file!</p>\n\n<p>I mainly seek enlightenment here :-) because the first app is a unit test, and it's unfortunate to have a bug that doesn't appear in the test but in the real app...</p>\n\n<p>(PS: I have fixed this specific bug, indeed by declaring the specialization in the header; but what other similar bugs might still be hidden?)</p>\n"}, {'answer_id': 59394, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': "<p>Per the specs, your specialized function template should never be called outside <code>fileA.C</code>, unless you <code>export</code> the template definition, which no compiler (except Comeau) currently supports (or has it planned for the forseeable future).</p>\n\n<p>On the other hand, once the function template is instantiated, there is a function visible to the compiler that is no longer a template. GCC may re-use this definition across different compiler units because the standard states that each template shall only be instantiated once for a given set of type arguments [temp.spec]. Still, since the template is not exported, this should be limited to the compilation unit.</p>\n\n<p>I believe that GCC may expose a bug here in sharing its list of instantiated templates across compilation units. Normally, this is a reasonable optimization but it should take function specializations into account which it doesn't seem to do correctly.</p>\n"}, {'answer_id': 59409, 'author': 'Mark Ransom', 'author_id': 5987, 'author_profile': 'https://Stackoverflow.com/users/5987', 'pm_score': 1, 'selected': False, 'text': '<p>In Microsoft C++, I did an experiment with inline functions.  I wanted to know what would happen if I defined incompatible versions of a function in different sources.  I got different results depending on whether I was using a Debug build or a Release build.  In Debug, the compiler refuses to inline anything, and the linker was linking the same version of the function no matter what was in scope in the source.  In Release, the compiler inlined whichever version had been defined at the time, and you got differing versions of the function.</p>\n\n<p>In neither case were there any warnings.  I kind of suspected this, which is why I did the experiment.</p>\n\n<p>I assume that template functions would behave the same, as would other compilers.</p>\n'}, {'answer_id': 59416, 'author': 'Anthony Williams', 'author_id': 5597, 'author_profile': 'https://Stackoverflow.com/users/5597', 'pm_score': 5, 'selected': False, 'text': '<p>It is <strong>an error</strong> to have a specialization for a template which is not visible at the point of call. Unfortunately, compilers are not required to diagnose this error, and can then do what they like with your code (in standardese it is "ill formed, no diagnostic required").</p>\n\n<p>Technically, you need to define the specialization in the header file, but just about every compiler will handle this as you might expect: this is fixed in C++11 with the new "extern template" facility:</p>\n\n<pre><code>extern template&lt;&gt; SomeFunc&lt;int&gt;();\n</code></pre>\n\n<p>This explicitly declares that the particular specialization is defined elsewhere. Many compilers support this already, some with and some without the <code>extern</code>.</p>\n'}, {'answer_id': 59458, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 0, 'selected': False, 'text': "<p>@[anthony-williams],</p>\n\n<p>are you sure you're not confusing <code>extern</code> template declarations with <code>extern template</code> instantiations? From what I see, <code>extern template</code> may <em>only</em> be used for explicit instantiation, not for specialization (which implies implicit instantiation). [temp.expl.spec] doesn't mention the <code>extern</code> keyword:</p>\n\n<blockquote>\n  <p><em>explicit-specialization</em>:<br>\n  &nbsp;&nbsp;&nbsp;&nbsp;<code>template</code> &lt; > <em>declaration</em></p>\n</blockquote>\n"}, {'answer_id': 787102, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I had the same problem with gcc4, here is how i solved it.  It was more simple a solution than what i was lead to believe by previous comments.  The previous posts ideas were correct but their syntax didn\'t work for me.</p>\n\n<pre><code>\n    ----------header-----------------\n    template &lt; class A &gt;\n    void foobar(A& object)\n    {\n      std::cout &lt;&lt; object;\n    }\n\n    template &lt;&gt; \n    void foobar(int);\n\n    ---------source------------------\n    #include "header.hpp"\n\n    template &lt;&gt;\n    void foobar(int x)\n    {\n      std::cout &lt;&lt; "an int";\n    }\n\n</code></pre>\n'}, {'answer_id': 18742483, 'author': 'CarLuva', 'author_id': 967077, 'author_profile': 'https://Stackoverflow.com/users/967077', 'pm_score': 1, 'selected': False, 'text': '<p>As Anthony Williams says, the <code>extern template</code> construct is the correct way to do this, but since his sample code is incomplete and has multiple syntax errors, here\'s a complete solution.</p>\n\n<p>fileA.h:</p>\n\n<pre><code>namespace myNamespace {\n  class classA {\n    public:\n      template &lt;class T&gt; void SomeFunc() { ... }\n  };\n\n  // The following line declares the specialization SomeFunc&lt;int&gt;().\n  template &lt;&gt; void classA::SomeFunc&lt;int&gt;();\n\n  // The following line externalizes the instantiation of the previously\n  // declared specialization SomeFunc&lt;int&gt;(). If the preceding line is omitted,\n  // the following line PREVENTS the specialization of SomeFunc&lt;int&gt;();\n  // SomeFunc&lt;int&gt;() will not be usable unless it is manually instantiated\n  // separately). When the preceding line is included, all the compilers I\n  // tested this on, including gcc, behave exactly the same (throwing a link\n  // error if the specialization of SomeFunc&lt;int&gt;() is not instantiated\n  // separately), regardless of whether or not the following line is included;\n  // however, my understanding is that nothing in the standard requires that\n  // behavior if the following line is NOT included.\n  extern template void classA::SomeFunc&lt;int&gt;();\n}\n</code></pre>\n\n<p>fileA.C:</p>\n\n<pre><code>#include "fileA.h"\n\ntemplate &lt;&gt; void myNamespace::classA::SomeFunc&lt;int&gt;() { ... }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/59331', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2148773/']