No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

214 líneas
10KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Template Instantiation (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Template Instantiation (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Template Instantiation (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Template-Instantiation"></a>
  62. <div class="header">
  63. <p>
  64. 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>
  65. </div>
  66. <hr>
  67. <a name="Where_0027s-the-Template_003f"></a>
  68. <h3 class="section">7.5 Where&rsquo;s the Template?</h3>
  69. <a name="index-template-instantiation"></a>
  70. <p>C++ templates were the first language feature to require more
  71. intelligence from the environment than was traditionally found on a UNIX
  72. system. Somehow the compiler and linker have to make sure that each
  73. template instance occurs exactly once in the executable if it is needed,
  74. and not at all otherwise. There are two basic approaches to this
  75. problem, which are referred to as the Borland model and the Cfront model.
  76. </p>
  77. <dl compact="compact">
  78. <dt>Borland model</dt>
  79. <dd><p>Borland C++ solved the template instantiation problem by adding the code
  80. equivalent of common blocks to their linker; the compiler emits template
  81. instances in each translation unit that uses them, and the linker
  82. collapses them together. The advantage of this model is that the linker
  83. only has to consider the object files themselves; there is no external
  84. complexity to worry about. The disadvantage is that compilation time
  85. is increased because the template code is being compiled repeatedly.
  86. Code written for this model tends to include definitions of all
  87. templates in the header file, since they must be seen to be
  88. instantiated.
  89. </p>
  90. </dd>
  91. <dt>Cfront model</dt>
  92. <dd><p>The AT&amp;T C++ translator, Cfront, solved the template instantiation
  93. problem by creating the notion of a template repository, an
  94. automatically maintained place where template instances are stored. A
  95. more modern version of the repository works as follows: As individual
  96. object files are built, the compiler places any template definitions and
  97. instantiations encountered in the repository. At link time, the link
  98. wrapper adds in the objects in the repository and compiles any needed
  99. instances that were not previously emitted. The advantages of this
  100. model are more optimal compilation speed and the ability to use the
  101. system linker; to implement the Borland model a compiler vendor also
  102. needs to replace the linker. The disadvantages are vastly increased
  103. complexity, and thus potential for error; for some code this can be
  104. just as transparent, but in practice it can been very difficult to build
  105. multiple programs in one directory and one program in multiple
  106. directories. Code written for this model tends to separate definitions
  107. of non-inline member templates into a separate file, which should be
  108. compiled separately.
  109. </p></dd>
  110. </dl>
  111. <p>G++ implements the Borland model on targets where the linker supports it,
  112. including ELF targets (such as GNU/Linux), Mac OS X and Microsoft Windows.
  113. Otherwise G++ implements neither automatic model.
  114. </p>
  115. <p>You have the following options for dealing with template instantiations:
  116. </p>
  117. <ol>
  118. <li> Do nothing. Code written for the Borland model works fine, but
  119. each translation unit contains instances of each of the templates it
  120. uses. The duplicate instances will be discarded by the linker, but in
  121. a large program, this can lead to an unacceptable amount of code
  122. duplication in object files or shared libraries.
  123. <p>Duplicate instances of a template can be avoided by defining an explicit
  124. instantiation in one object file, and preventing the compiler from doing
  125. implicit instantiations in any other object files by using an explicit
  126. instantiation declaration, using the <code>extern template</code> syntax:
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">extern template int max (int, int);
  130. </pre></div>
  131. <p>This syntax is defined in the C++ 2011 standard, but has been supported by
  132. G++ and other compilers since well before 2011.
  133. </p>
  134. <p>Explicit instantiations can be used for the largest or most frequently
  135. duplicated instances, without having to know exactly which other instances
  136. are used in the rest of the program. You can scatter the explicit
  137. instantiations throughout your program, perhaps putting them in the
  138. translation units where the instances are used or the translation units
  139. that define the templates themselves; you can put all of the explicit
  140. instantiations you need into one big file; or you can create small files
  141. like
  142. </p>
  143. <div class="smallexample">
  144. <pre class="smallexample">#include &quot;Foo.h&quot;
  145. #include &quot;Foo.cc&quot;
  146. template class Foo&lt;int&gt;;
  147. template ostream&amp; operator &lt;&lt;
  148. (ostream&amp;, const Foo&lt;int&gt;&amp;);
  149. </pre></div>
  150. <p>for each of the instances you need, and create a template instantiation
  151. library from those.
  152. </p>
  153. <p>This is the simplest option, but also offers flexibility and
  154. fine-grained control when necessary. It is also the most portable
  155. alternative and programs using this approach will work with most modern
  156. compilers.
  157. </p>
  158. </li><li> <a name="index-fno_002dimplicit_002dtemplates-1"></a>
  159. Compile your code with <samp>-fno-implicit-templates</samp> to disable the
  160. implicit generation of template instances, and explicitly instantiate
  161. all the ones you use. This approach requires more knowledge of exactly
  162. which instances you need than do the others, but it&rsquo;s less
  163. mysterious and allows greater control if you want to ensure that only
  164. the intended instances are used.
  165. <p>If you are using Cfront-model code, you can probably get away with not
  166. using <samp>-fno-implicit-templates</samp> when compiling files that don&rsquo;t
  167. &lsquo;<samp>#include</samp>&rsquo; the member template definitions.
  168. </p>
  169. <p>If you use one big file to do the instantiations, you may want to
  170. compile it without <samp>-fno-implicit-templates</samp> so you get all of the
  171. instances required by your explicit instantiations (but not by any
  172. other files) without having to specify them as well.
  173. </p>
  174. <p>In addition to forward declaration of explicit instantiations
  175. (with <code>extern</code>), G++ has extended the template instantiation
  176. syntax to support instantiation of the compiler support data for a
  177. template class (i.e. the vtable) without instantiating any of its
  178. members (with <code>inline</code>), and instantiation of only the static data
  179. members of a template class, without the support data or member
  180. functions (with <code>static</code>):
  181. </p>
  182. <div class="smallexample">
  183. <pre class="smallexample">inline template class Foo&lt;int&gt;;
  184. static template class Foo&lt;int&gt;;
  185. </pre></div>
  186. </li></ol>
  187. <hr>
  188. <div class="header">
  189. <p>
  190. 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>
  191. </div>
  192. </body>
  193. </html>