<p>C++ seems to be rather grouchy when declaring templates across multiple files. More specifically, when working with templated classes, the linker expect all method definitions for the class in a single compiler object file. When you take into account headers, other declarations, inheritance, etc., things get really messy. </p>

<p>Are there any general advice or workarounds for organizing or redistributing templated member definitions across multiple files?</p>

[{'answer_id': 36042, 'author': 'Eric Scrivner', 'author_id': 2594, 'author_profile': 'https://Stackoverflow.com/users/2594', 'pm_score': 3, 'selected': False, 'text': '<p>Across how many files?  If you just want to separate class definitions from implementation then try <a href="https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl" rel="nofollow noreferrer">this article</a> in the C++ faqs.  That\'s about the only way I know of that works at the moment, but some IDEs (Eclipse CDT for example) won\'t link this method properly and you may get a lot of errors.  However, writing your own makefiles or using Visual C++ has always worked for me :-)</p>\n'}, {'answer_id': 36080, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 6, 'selected': True, 'text': '<blockquote>\n  <p>Are there any general advice or workarounds for organizing or redistributing templated member definitions across multiple files?</p>\n</blockquote>\n\n<p>Yes; don\'t.</p>\n\n<p>The C++ spec permits a compiler to be able to "see" the entire template (declaration <em>and</em> definition) at the point of instantiation, and (due to the complexities of any implementation) most compilers retain this requirement.  The upshot is that #inclusion of any template header must also #include any and all source required to instantiate the template.</p>\n\n<p>The easiest way to deal with this is to dump everything into the header, inline where posible, out-of-line where necessary.</p>\n\n<p>If you really regard this as an unacceptable affront, a common option is to split the template into the usual header/implementation pair, and then #include the implementation file at the end of the header.</p>\n\n<p>C++\'s "export" feature may or may not provide another workaround. The feature is poorly supported and poorly defined; although it in principle should permit some kind of separate compilation of templates, it doesn\'t necessarily obviate the demand that the compiler be able to see the entire template body.</p>\n'}, {'answer_id': 94890, 'author': 'jwfearn', 'author_id': 10559, 'author_profile': 'https://Stackoverflow.com/users/10559', 'pm_score': 2, 'selected': False, 'text': '<p>When/if your compiler supports C++0x, the <code>extern</code> keyword can be used to separate template declarations from definitions.</p>\n\n<p>See <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Extern_template" rel="nofollow noreferrer">here</a> for a brief explanation.</p>\n\n<p>Also, section 6.3, "The Separation Model," of <em>C++ Templates: The Complete Guide</em> by David Vandevoorde and Nicolai M. Josuttis describes other options.</p>\n\n<p><img src="https://i.stack.imgur.com/fD0vx.gif" alt="alt text"></p>\n'}]
['https://Stackoverflow.com/questions/36039', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4381/']