{"id":170,"date":"2014-03-31T01:36:50","date_gmt":"2014-03-30T17:36:50","guid":{"rendered":"https:\/\/gyf.blog.ustc.edu.cn\/?p=170"},"modified":"2014-03-31T01:36:50","modified_gmt":"2014-03-30T17:36:50","slug":"cpp-template-class-file","status":"publish","type":"post","link":"https:\/\/blog.yfgao.com\/zh\/2014\/03\/31\/cpp-template-class-file\/","title":{"rendered":"[\u8bd1]c++\u4e2d\u6a21\u677f\u7c7b\u58f0\u660e\u4e0e\u5b9e\u73b0\u5206\u79bb\u7684\u5904\u7406\u65b9\u6cd5"},"content":{"rendered":"<p><strong><a href=\"http:\/\/stackoverflow.com\/questions\/495021\/why-can-templates-only-be-implemented-in-the-header-file\">\u67e5\u770b\u539f\u6587<\/a><\/strong><\/p>\n<p>when instantiating a template, the compiler creates a new class with the given template argument.<\/p>\n<p>\u5f53\u521d\u59cb\u5316\u4e00\u4e2a\u6a21\u677f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u7528\u7ed9\u5b9a\u7684\u53c2\u6570\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u7c7b<\/p>\n<p>For example<\/p>\n<p>\u6bd4\u5982\uff1a<\/p>\n<pre><code>template&lt;typename T&gt;\nstruct Foo\n{\n    T bar;\n    void doSomething(T param) {\/* do stuff using T *\/}\n};\n\n\/\/ somewhere in a .cpp\nFoo&lt;int&gt; f;\n<\/code><\/pre>\n<p>When reading this line, the compiler will create a new class (let&#8217;s call it FooInt), which is equivalent to the following:<\/p>\n<p>\u5f53\u7f16\u8bd1\u5230\u8fd9\u884c\u65f6,\u7f16\u8bd1\u5668\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7c7b(\u8fd9\u91cc\u6743\u4e14\u79f0\u4e3aFooInt),\u5b83\u4e0e\u4ee5\u4e0b\u4ee3\u7801\u7b49\u4ef7\uff1a<\/p>\n<pre><code>struct FooInt\n{\n    int bar;\n    void doSomething(int param) {\/* do stuff using int *\/}\n}\n<\/code><\/pre>\n<p>Consequently, the compiler needs to have access to the implementation of the methods, to instantiate them with the template argument (in this case int). If these implementations were not in the header, they wouldn&#8217;t be accessible, and therefore the compiler wouldn&#8217;t be able to instantiate the template.<\/p>\n<p>\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u9700\u8981\u626b\u63cf\u6240\u6709\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0c\u4ee5\u4fbf\u80fd\u591f\u7528\u6a21\u677f\u53c2\u6570(\u8fd9\u91cc\u662fint)\u5b9e\u4f8b\u5316\u8fd9\u4e9b\u7c7b\u3002\u5982\u679c\u8fd9\u4e9b\u5b9e\u4f8b\u4e0d\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u4ed6\u4eec\u5c06\u65e0\u6cd5\u88ab\u8bbf\u95ee\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4e5f\u65e0\u6cd5\u5b9e\u4f8b\u5316\u6a21\u677f\u3002<\/p>\n<p>A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header.<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u7684\u901a\u7528\u89e3\u51b3\u65b9\u6cd5\u662f\u5728\u5934\u6587\u4ef6\u4e2d\u8fdb\u884c\u6a21\u677f\u58f0\u660e\uff0c\u7136\u540e\u5728\u5b9e\u4f8b\u6587\u4ef6\u4e2d\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u7c7b(\u6bd4\u5982 .tpp)\uff0c\u5e76\u4e14\u5c06\u8fd9\u4e2a\u5b9e\u4f8b\u6587\u4ef6\u5305\u542b\u5728\u5934\u6587\u4ef6\u7684\u672b\u5c3e\u3002<\/p>\n<pre><code>\/\/ Foo.h\ntemplate &lt;typename T&gt;\nstruct Foo\n{\n    void doSomething(T param);\n};\n\n#include \"Foo.tpp\"\n\n\/\/ Foo.tpp\ntemplate &lt;typename T&gt;\nvoid Foo&lt;T&gt;::doSomething(T param)\n{\n    \/\/implementation\n}\n<\/code><\/pre>\n<p>This way, implementation is still separated from declaration, but is accessible to the compiler.<\/p>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u5b9e\u73b0\u4f9d\u7136\u4e0e\u58f0\u660e\u76f8\u5206\u79bb\uff0c\u5e76\u4e14\u53ef\u4ee5\u7f16\u8bd1\u4e86\u3002<\/p>\n<p>Another solution is to keep the implementation separated, and explicitly instantiate all the template instances you&#8217;ll need:<\/p>\n<p>\u53e6\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u662f\uff0c\u4fdd\u6301\u5b9e\u73b0\u5206\u79bb\uff0c\u5e76\u4e14\u660e\u786e\u652f\u6301\u6240\u9700\u8981\u7684\u6240\u6709\u6a21\u677f\u5b9e\u4f8b\uff1a<\/p>\n<pre><code>\/\/ Foo.h\n\n\/\/ no implementation\ntemplate &lt;typename T&gt; struct Foo { ... };\n\n\/\/----------------------------------------\n\/\/ Foo.cpp\n\n\/\/ implementation of Foo's methods\n\n\/\/ explicit instantiations\ntemplate class Foo&lt;int&gt;;\ntemplate class Foo&lt;float&gt;;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u770b\u539f\u6587 when instantiating a template, the compiler create &hellip; <a href=\"https:\/\/blog.yfgao.com\/zh\/2014\/03\/31\/cpp-template-class-file\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">[\u8bd1]c++\u4e2d\u6a21\u677f\u7c7b\u58f0\u660e\u4e0e\u5b9e\u73b0\u5206\u79bb\u7684\u5904\u7406\u65b9\u6cd5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"translation":{"provider":"WPGlobus","version":"2.12.2","language":"zh","enabled_languages":["en","zh"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"zh":{"title":false,"content":false,"excerpt":false}}},"_links":{"self":[{"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/posts\/170"}],"collection":[{"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":0,"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.yfgao.com\/zh\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}