📄 template-instantiation.html
字号:
<html lang="en"><head><title>Using the GNU Compiler Collection (GCC)</title><meta http-equiv="Content-Type" content="text/html"><meta name="description" content="Using the GNU Compiler Collection (GCC)"><meta name="generator" content="makeinfo 4.6"><!--Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below). A copy of the license isincluded in the section entitled "GNU Free Documentation License". <p>(a) The FSF's Front-Cover Text is: <p>A GNU Manual <p>(b) The FSF's Back-Cover Text is: <p>You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller }--></style></head><body><div class="node"><p>Node: <a name="Template%20Instantiation">Template Instantiation</a>,Next: <a rel="next" accesskey="n" href="Bound-member-functions.html#Bound%20member%20functions">Bound member functions</a>,Previous: <a rel="previous" accesskey="p" href="C---Interface.html#C++%20Interface">C++ Interface</a>,Up: <a rel="up" accesskey="u" href="C---Extensions.html#C++%20Extensions">C++ Extensions</a><hr><br></div><h3 class="section">Where's the Template?</h3><p>C++ templates are the first language feature to require moreintelligence from the environment than one usually finds on a UNIXsystem. Somehow the compiler and linker have to make sure that eachtemplate instance occurs exactly once in the executable if it is needed,and not at all otherwise. There are two basic approaches to thisproblem, which I will refer to as the Borland model and the Cfront model. <dl><dt>Borland model <dd>Borland C++ solved the template instantiation problem by adding the codeequivalent of common blocks to their linker; the compiler emits templateinstances in each translation unit that uses them, and the linkercollapses them together. The advantage of this model is that the linkeronly has to consider the object files themselves; there is no externalcomplexity to worry about. This disadvantage is that compilation timeis increased because the template code is being compiled repeatedly. Code written for this model tends to include definitions of alltemplates in the header file, since they must be seen to beinstantiated. <br><dt>Cfront model <dd>The AT&T C++ translator, Cfront, solved the template instantiationproblem by creating the notion of a template repository, anautomatically maintained place where template instances are stored. Amore modern version of the repository works as follows: As individualobject files are built, the compiler places any template definitions andinstantiations encountered in the repository. At link time, the linkwrapper adds in the objects in the repository and compiles any neededinstances that were not previously emitted. The advantages of thismodel are more optimal compilation speed and the ability to use thesystem linker; to implement the Borland model a compiler vendor alsoneeds to replace the linker. The disadvantages are vastly increasedcomplexity, and thus potential for error; for some code this can bejust as transparent, but in practice it can been very difficult to buildmultiple programs in one directory and one program in multipledirectories. Code written for this model tends to separate definitionsof non-inline member templates into a separate file, which should becompiled separately. </dl> <p>When used with GNU ld version 2.8 or later on an ELF system such asGNU/Linux or Solaris 2, or on Microsoft Windows, G++ supports theBorland model. On other systems, G++ implements neither automaticmodel. <p>A future version of G++ will support a hybrid model whereby the compilerwill emit any instantiations for which the template definition isincluded in the compile, and store template definitions andinstantiation context information into the object file for the rest. The link wrapper will extract that information as necessary and invokethe compiler to produce the remaining instantiations. The linker willthen combine duplicate instantiations. <p>In the mean time, you have the following options for dealing withtemplate instantiations: <ol type=1 start=1><li>Compile your template-using code with <code>-frepo</code>. The compiler willgenerate files with the extension <code>.rpo</code> listing all of thetemplate instantiations used in the corresponding object files whichcould be instantiated there; the link wrapper, <code>collect2</code>, willthen update the <code>.rpo</code> files to tell the compiler where to placethose instantiations and rebuild any affected object files. Thelink-time overhead is negligible after the first pass, as the compilerwill continue to place the instantiations in the same files. <p>This is your best option for application code written for the Borlandmodel, as it will just work. Code written for the Cfront model willneed to be modified so that the template definitions are available atone or more points of instantiation; usually this is as simple as adding<code>#include <tmethods.cc></code> to the end of each template header. <p>For library code, if you want the library to provide all of the templateinstantiations it needs, just try to link all of its object filestogether; the link will fail, but cause the instantiations to begenerated as a side effect. Be warned, however, that this may causeconflicts if multiple libraries try to provide the same instantiations. For greater control, use explicit instantiation as described in the nextoption. </p><li>Compile your code with <code>-fno-implicit-templates</code> to disable theimplicit generation of template instances, and explicitly instantiateall the ones you use. This approach requires more knowledge of exactlywhich instances you need than do the others, but it's lessmysterious and allows greater control. You can scatter the explicitinstantiations throughout your program, perhaps putting them in thetranslation units where the instances are used or the translation unitsthat define the templates themselves; you can put all of the explicitinstantiations you need into one big file; or you can create small fileslike <pre class="smallexample"> #include "Foo.h" #include "Foo.cc" template class Foo<int>; template ostream& operator << (ostream&, const Foo<int>&); </pre> <p>for each of the instances you need, and create a template instantiationlibrary from those. <p>If you are using Cfront-model code, you can probably get away with notusing <code>-fno-implicit-templates</code> when compiling files that don't<code>#include</code> the member template definitions. <p>If you use one big file to do the instantiations, you may want tocompile it without <code>-fno-implicit-templates</code> so you get all of theinstances required by your explicit instantiations (but not by anyother files) without having to specify them as well. <p>G++ has extended the template instantiation syntax given in the ISOstandard to allow forward declaration of explicit instantiations(with <code>extern</code>), instantiation of the compiler support data for atemplate class (i.e. the vtable) without instantiating any of itsmembers (with <code>inline</code>), and instantiation of only the static datamembers of a template class, without the support data or memberfunctions (with (<code>static</code>): <pre class="smallexample"> extern template int max (int, int); inline template class Foo<int>; static template class Foo<int>; </pre> </p><li>Do nothing. Pretend G++ does implement automatic instantiationmanagement. Code written for the Borland model will work fine, buteach translation unit will contain instances of each of the templates ituses. In a large program, this can lead to an unacceptable amount of codeduplication. <p>See <a href="C---Interface.html#C++%20Interface">Declarations and Definitions in One Header</a>, formore discussion of these pragmas. </ol> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -