Template-Instantiation.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Template Instantiation</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Template Instantiation">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Template Instantiation">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" rel="up" title="C++ Extensions">
  30. <link href="Bound-member-functions.html#Bound-member-functions" rel="next" title="Bound member functions">
  31. <link href="C_002b_002b-Interface.html#C_002b_002b-Interface" rel="prev" title="C++ Interface">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Template-Instantiation"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Bound-member-functions.html#Bound-member-functions" accesskey="n" rel="next">Bound member functions</a>, Previous: <a href="C_002b_002b-Interface.html#C_002b_002b-Interface" accesskey="p" rel="prev">C++ Interface</a>, Up: <a href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" accesskey="u" rel="up">C++ Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Where_0027s-the-Template_003f"></a>
  69. <h3 class="section">7.5 Where&rsquo;s the Template?</h3>
  70. <a name="index-template-instantiation"></a>
  71. <p>C++ templates were the first language feature to require more
  72. intelligence from the environment than was traditionally found on a UNIX
  73. system. Somehow the compiler and linker have to make sure that each
  74. template instance occurs exactly once in the executable if it is needed,
  75. and not at all otherwise. There are two basic approaches to this
  76. problem, which are referred to as the Borland model and the Cfront model.
  77. </p>
  78. <dl compact="compact">
  79. <dt>Borland model</dt>
  80. <dd><p>Borland C++ solved the template instantiation problem by adding the code
  81. equivalent of common blocks to their linker; the compiler emits template
  82. instances in each translation unit that uses them, and the linker
  83. collapses them together. The advantage of this model is that the linker
  84. only has to consider the object files themselves; there is no external
  85. complexity to worry about. The disadvantage is that compilation time
  86. is increased because the template code is being compiled repeatedly.
  87. Code written for this model tends to include definitions of all
  88. templates in the header file, since they must be seen to be
  89. instantiated.
  90. </p>
  91. </dd>
  92. <dt>Cfront model</dt>
  93. <dd><p>The AT&amp;T C++ translator, Cfront, solved the template instantiation
  94. problem by creating the notion of a template repository, an
  95. automatically maintained place where template instances are stored. A
  96. more modern version of the repository works as follows: As individual
  97. object files are built, the compiler places any template definitions and
  98. instantiations encountered in the repository. At link time, the link
  99. wrapper adds in the objects in the repository and compiles any needed
  100. instances that were not previously emitted. The advantages of this
  101. model are more optimal compilation speed and the ability to use the
  102. system linker; to implement the Borland model a compiler vendor also
  103. needs to replace the linker. The disadvantages are vastly increased
  104. complexity, and thus potential for error; for some code this can be
  105. just as transparent, but in practice it can been very difficult to build
  106. multiple programs in one directory and one program in multiple
  107. directories. Code written for this model tends to separate definitions
  108. of non-inline member templates into a separate file, which should be
  109. compiled separately.
  110. </p></dd>
  111. </dl>
  112. <p>G++ implements the Borland model on targets where the linker supports it,
  113. including ELF targets (such as GNU/Linux), Mac OS X and Microsoft Windows.
  114. Otherwise G++ implements neither automatic model.
  115. </p>
  116. <p>You have the following options for dealing with template instantiations:
  117. </p>
  118. <ol>
  119. <li> Do nothing. Code written for the Borland model works fine, but
  120. each translation unit contains instances of each of the templates it
  121. uses. The duplicate instances will be discarded by the linker, but in
  122. a large program, this can lead to an unacceptable amount of code
  123. duplication in object files or shared libraries.
  124. <p>Duplicate instances of a template can be avoided by defining an explicit
  125. instantiation in one object file, and preventing the compiler from doing
  126. implicit instantiations in any other object files by using an explicit
  127. instantiation declaration, using the <code>extern template</code> syntax:
  128. </p>
  129. <div class="smallexample">
  130. <pre class="smallexample">extern template int max (int, int);
  131. </pre></div>
  132. <p>This syntax is defined in the C++ 2011 standard, but has been supported by
  133. G++ and other compilers since well before 2011.
  134. </p>
  135. <p>Explicit instantiations can be used for the largest or most frequently
  136. duplicated instances, without having to know exactly which other instances
  137. are used in the rest of the program. You can scatter the explicit
  138. instantiations throughout your program, perhaps putting them in the
  139. translation units where the instances are used or the translation units
  140. that define the templates themselves; you can put all of the explicit
  141. instantiations you need into one big file; or you can create small files
  142. like
  143. </p>
  144. <div class="smallexample">
  145. <pre class="smallexample">#include &quot;Foo.h&quot;
  146. #include &quot;Foo.cc&quot;
  147. template class Foo&lt;int&gt;;
  148. template ostream&amp; operator &lt;&lt;
  149. (ostream&amp;, const Foo&lt;int&gt;&amp;);
  150. </pre></div>
  151. <p>for each of the instances you need, and create a template instantiation
  152. library from those.
  153. </p>
  154. <p>This is the simplest option, but also offers flexibility and
  155. fine-grained control when necessary. It is also the most portable
  156. alternative and programs using this approach will work with most modern
  157. compilers.
  158. </p>
  159. </li><li> <a name="index-frepo-1"></a>
  160. Compile your template-using code with <samp>-frepo</samp>. The compiler
  161. generates files with the extension &lsquo;<samp>.rpo</samp>&rsquo; listing all of the
  162. template instantiations used in the corresponding object files that
  163. could be instantiated there; the link wrapper, &lsquo;<samp>collect2</samp>&rsquo;,
  164. then updates the &lsquo;<samp>.rpo</samp>&rsquo; files to tell the compiler where to place
  165. those instantiations and rebuild any affected object files. The
  166. link-time overhead is negligible after the first pass, as the compiler
  167. continues to place the instantiations in the same files.
  168. <p>This can be a suitable option for application code written for the Borland
  169. model, as it usually just works. Code written for the Cfront model
  170. needs to be modified so that the template definitions are available at
  171. one or more points of instantiation; usually this is as simple as adding
  172. <code>#include &lt;tmethods.cc&gt;</code> to the end of each template header.
  173. </p>
  174. <p>For library code, if you want the library to provide all of the template
  175. instantiations it needs, just try to link all of its object files
  176. together; the link will fail, but cause the instantiations to be
  177. generated as a side effect. Be warned, however, that this may cause
  178. conflicts if multiple libraries try to provide the same instantiations.
  179. For greater control, use explicit instantiation as described in the next
  180. option.
  181. </p>
  182. </li><li> <a name="index-fno_002dimplicit_002dtemplates-1"></a>
  183. Compile your code with <samp>-fno-implicit-templates</samp> to disable the
  184. implicit generation of template instances, and explicitly instantiate
  185. all the ones you use. This approach requires more knowledge of exactly
  186. which instances you need than do the others, but it&rsquo;s less
  187. mysterious and allows greater control if you want to ensure that only
  188. the intended instances are used.
  189. <p>If you are using Cfront-model code, you can probably get away with not
  190. using <samp>-fno-implicit-templates</samp> when compiling files that don&rsquo;t
  191. &lsquo;<samp>#include</samp>&rsquo; the member template definitions.
  192. </p>
  193. <p>If you use one big file to do the instantiations, you may want to
  194. compile it without <samp>-fno-implicit-templates</samp> so you get all of the
  195. instances required by your explicit instantiations (but not by any
  196. other files) without having to specify them as well.
  197. </p>
  198. <p>In addition to forward declaration of explicit instantiations
  199. (with <code>extern</code>), G++ has extended the template instantiation
  200. syntax to support instantiation of the compiler support data for a
  201. template class (i.e. the vtable) without instantiating any of its
  202. members (with <code>inline</code>), and instantiation of only the static data
  203. members of a template class, without the support data or member
  204. functions (with <code>static</code>):
  205. </p>
  206. <div class="smallexample">
  207. <pre class="smallexample">inline template class Foo&lt;int&gt;;
  208. static template class Foo&lt;int&gt;;
  209. </pre></div>
  210. </li></ol>
  211. <hr>
  212. <div class="header">
  213. <p>
  214. Next: <a href="Bound-member-functions.html#Bound-member-functions" accesskey="n" rel="next">Bound member functions</a>, Previous: <a href="C_002b_002b-Interface.html#C_002b_002b-Interface" accesskey="p" rel="prev">C++ Interface</a>, Up: <a href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" accesskey="u" rel="up">C++ Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  215. </div>
  216. </body>
  217. </html>