source: trunk/third/gcc/cp/templates.texi @ 8834

Revision 8834, 8.1 KB checked in by ghudson, 28 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r8833, which included commits to RCS files with non-trunk default branches.
Line 
1@node Templates
2@chapter The Template Implementation
3
4@cindex templates
5@cindex function templates
6@cindex class templates
7@cindex parameterized types
8@cindex types, parameterized
9The C++ template@footnote{Class templates are also known as
10@dfn{parameterized types}.} facility, which effectively allows use of
11variables for types in declarations, is one of the newest features of
12the language.
13
14@sc{gnu} C++ is one of the first compilers to implement many
15of the template facilities currently defined by the @sc{ansi} committee.
16
17Nevertheless, the template implementation is not yet complete.  This
18chapter maps the current limitations of the @sc{gnu} C++ template
19implementation.
20
21@menu
22* Template limitations:: Limitations for function and class templates
23* Function templates::   Limitations for function templates
24* Class templates::      Limitations for class templates
25* Template debugging::   Debugging information for templates
26@end menu
27
28@node Template limitations
29@section Limitations for function and class templates
30
31@cindex template limitations
32@cindex template bugs
33@cindex bugs, templates
34These limitations apply to any use of templates (function templates or
35class templates) with @sc{gnu} C++:
36
37@table @emph
38@item Template definitions must be visible
39When you compile code with templates, the template definitions must come
40first (before the compiler needs to expand them), and template
41definitions you use must be visible in the current scope.
42@c FIXME! Is this a defined property of templates, rather than a
43@c temporary limitation?
44@c ANSWER: It's a limitation, but it's hard to say why it's a limitation
45@c to someone.  We need an infinite link-cycle, in one camp, to
46@c accomplish things so you don't need the template definitions around.
47
48@cindex static data in template classes
49@cindex template classes, static data in
50@item Individual initializers needed for static data
51Templates for static data in template classes do not work.  @xref{Class
52templates,,Limitations for class templates}.
53@end table
54
55@node Function templates
56@section Limitations for function templates
57
58@cindex function template limitations
59Function templates are implemented for the most part.  The compiler can
60correctly determine template parameter values, and will delay
61instantiation of a function that uses templates until the requisite type
62information is available.
63
64@noindent
65The following limitations remain:
66
67@itemize @bullet
68@cindex template vs declaration, functions
69@cindex declaration vs template, functions
70@cindex function declaration vs template
71@item
72Narrowed specification: function declarations should not prevent
73template expansion.  When you declare a function, @sc{gnu} C++
74interprets the declaration as an indication that you will provide a
75definition for that function.  Therefore, @sc{gnu} C++ does not use a
76template expansion if there is also an applicable declaration.  @sc{gnu}
77C++ only expands the template when there is no such declaration.
78
79The specification in Bjarne Stroustrup's @cite{The C++ Programming
80Language, Second Edition} is narrower, and the @sc{gnu} C++
81implementation is now clearly incorrect.  With this new specification, a
82declaration that corresponds to an instantiation of a function template
83only affects whether conversions are needed to use that version of the
84function.  It should no longer prevent expansion of the template
85definition.
86
87For example, this code fragment must be treated differently:
88
89@smallexample
90template <class X> X min (X& x1, X& x2) @{ @dots{} @}
91int min (int, int);
92@dots{}
93int i; short s;
94min (i, s); // @r{should call} min(int,int)
95            // @r{derived from template}
96@dots{}
97@end smallexample
98
99@item
100The compiler does not yet understand function signatures where types are
101nested within template parameters.  For example, a function like the
102following produces a syntax error on the closing @samp{)} of the
103definition of the function @code{f}:
104
105@smallexample
106template <class T> class A @{ public: T x; class Y @{@}; @};
107template <class X> int f (A<X>::Y y) @{ @dots{} @}
108@end smallexample
109
110@cindex @code{inline} and function templates
111@cindex function templates and @code{inline}
112@item
113If you declare an @code{inline} function using templates, the compiler
114can only inline the code @emph{after} the first time you use
115that function with whatever particular type signature the template
116was instantiated.
117
118Removing this limitation is akin to supporting nested function
119definitions in @sc{gnu} C++; the limitation will probably remain until the
120more general problem of nested functions is solved.
121
122@item
123All the @emph{method} templates (templates for member functions) for a
124class must be visible to the compiler when the class template is
125instantiated.
126@end itemize
127
128@node Class templates
129@section Limitations for class templates
130
131@cindex class template limitations
132@ignore
133FIXME!!  Include a comprehensible version of this if someone can explain it.
134         (Queried Brendan and Raeburn w/full orig context, 26may1993---pesch)
135   - [RHP: I don't understand what the following fragment refers to.  If it's
136     the "BIG BUG" section in the original, why does it say "overriding class
137     declarations" here when the more detailed text refers to *function*
138     declarations?  Here's the fragment I don't understand:]
139     there are problems with user-supplied overriding class declarations (see
140     below).
141@end ignore
142
143@itemize @bullet
144@ignore
145@cindex static data, not working in templates
146@item
147Templates for static data in template classes do not work.
148Currently, you must initialize each case of such data
149individually.
150@c FIXME!! Brendan to see if still true.
151@c ANSWER: This section presumes that it's incorrect to have to
152@c initialize for each type you instantiate with.  It's not, it's the
153@c right way to do it.
154@end ignore
155
156Unfortunately, individual initializations of this sort are likely to be
157considered errors eventually; since they're needed now, you might want to
158flag places where you use them with comments to mark the need for a
159future transition.
160
161@cindex nested type results vs templates
162@item
163Member functions in template classes may not have results of nested
164type; @sc{gnu} C++ signals a syntax error on the attempt.  The following
165example illustrates this problem with an @code{enum} type @code{alph}:
166
167@smallexample
168template <class T> class list @{
169  @dots{}
170  enum alph @{a,b,c@};
171  alph bar();
172  @dots{}
173@};
174
175template <class T>
176list<int>::alph list<int>::bar()  // @i{Syntax error here}
177@{
178@dots{}
179@}
180@end smallexample
181
182@cindex preprocessor conditionals in templates
183@cindex conditionals (preprocessor) in templates
184@item
185A parsing bug makes it difficult to use preprocessor conditionals within
186templates.  For example, in this code:
187
188@smallexample
189template <class T>
190class list @{
191  @dots{}
192#ifdef SYSWRONG
193  T x;
194#endif
195  @dots{}
196@}
197@end smallexample
198
199The preprocessor output leaves sourcefile line number information (lines
200like @samp{# 6 "foo.cc"} when it expands the @code{#ifdef} block.  These
201lines confuse the compiler while parsing templates, giving a syntax
202error.
203
204If you cannot avoid preprocessor conditionals in templates, you can
205suppress the line number information using the @samp{-P} preprocessor
206option (but this will make debugging more difficult), by compiling the
207affected modules like this:
208
209@smallexample
210g++ -P foo.cc -o foo
211@end smallexample
212
213@cindex parsing errors, templates
214@item
215Parsing errors are reported when templates are first
216@emph{instantiated}---not on the template definition itself.  In
217particular, if you do not instantiate a template definition at all, the
218compiler never reports any parsing errors that may be in the template
219definition.
220@end itemize
221
222@node Template debugging
223@section Debugging information for templates
224
225@cindex templates and debugging information
226@cindex debugging information and templates
227Debugging information for templates works for some object code formats,
228but not others.  It works for stabs@footnote{Except that insufficient
229debugging information for methods of template classes is generated in
230stabs.} (used primarily in @sc{a.out} object code, but also in the Solaris 2
231version of @sc{elf}), and the @sc{mips} version of @sc{coff} debugging
232format.
233
234@sc{dwarf} support is currently minimal, and requires further
235development.
Note: See TracBrowser for help on using the repository browser.