?? templates.texi
字號:
@node Templates@chapter The Template Implementation@cindex templates@cindex function templates@cindex class templates@cindex parameterized types@cindex types, parameterizedThe C++ template@footnote{Class templates are also known as@dfn{parameterized types}.} facility, which effectively allows use ofvariables for types in declarations, is one of the newest features ofthe language.@sc{gnu} C++ is one of the first compilers to implement manyof the template facilities currently defined by the @sc{ansi} committee.Nevertheless, the template implementation is not yet complete. Thischapter maps the current limitations of the @sc{gnu} C++ templateimplementation.@menu* Template limitations:: Limitations for function and class templates* Function templates:: Limitations for function templates* Class templates:: Limitations for class templates* Template debugging:: Debugging information for templates@end menu@node Template limitations@section Limitations for function and class templates@cindex template limitations@cindex template bugs@cindex bugs, templatesThese limitations apply to any use of templates (function templates orclass templates) with @sc{gnu} C++:@table @emph@item Template definitions must be visibleWhen you compile code with templates, the template definitions must comefirst (before the compiler needs to expand them), and templatedefinitions you use must be visible in the current scope.@c FIXME! Is this a defined property of templates, rather than a@c temporary limitation?@c ANSWER: It's a limitation, but it's hard to say why it's a limitation@c to someone. We need an infinite link-cycle, in one camp, to@c accomplish things so you don't need the template definitions around.@cindex static data in template classes@cindex template classes, static data in@item Individual initializers needed for static dataTemplates for static data in template classes do not work. @xref{Classtemplates,,Limitations for class templates}.@end table@node Function templates@section Limitations for function templates@cindex function template limitationsFunction templates are implemented for the most part. The compiler cancorrectly determine template parameter values, and will delayinstantiation of a function that uses templates until the requisite typeinformation is available.@noindentThe following limitations remain: @itemize @bullet@cindex template vs declaration, functions@cindex declaration vs template, functions@cindex function declaration vs template@itemNarrowed specification: function declarations should not preventtemplate expansion. When you declare a function, @sc{gnu} C++interprets the declaration as an indication that you will provide adefinition for that function. Therefore, @sc{gnu} C++ does not use atemplate expansion if there is also an applicable declaration. @sc{gnu}C++ only expands the template when there is no such declaration.The specification in Bjarne Stroustrup's @cite{The C++ ProgrammingLanguage, Second Edition} is narrower, and the @sc{gnu} C++implementation is now clearly incorrect. With this new specification, adeclaration that corresponds to an instantiation of a function templateonly affects whether conversions are needed to use that version of thefunction. It should no longer prevent expansion of the templatedefinition.For example, this code fragment must be treated differently:@smallexampletemplate <class X> X min (X& x1, X& x2) @{ @dots{} @}int min (int, int);@dots{}int i; short s;min (i, s); // @r{should call} min(int,int) // @r{derived from template}@dots{}@end smallexample@itemThe compiler does not yet understand function signatures where types arenested within template parameters. For example, a function like thefollowing produces a syntax error on the closing @samp{)} of thedefinition of the function @code{f}:@smallexampletemplate <class T> class A @{ public: T x; class Y @{@}; @};template <class X> int f (A<X>::Y y) @{ @dots{} @}@end smallexample@cindex @code{inline} and function templates@cindex function templates and @code{inline}@itemIf you declare an @code{inline} function using templates, the compilercan only inline the code @emph{after} the first time you usethat function with whatever particular type signature the templatewas instantiated.Removing this limitation is akin to supporting nested functiondefinitions in @sc{gnu} C++; the limitation will probably remain until themore general problem of nested functions is solved.@itemAll the @emph{method} templates (templates for member functions) for aclass must be visible to the compiler when the class template isinstantiated. @end itemize@node Class templates@section Limitations for class templates@cindex class template limitations@ignoreFIXME!! Include a comprehensible version of this if someone can explain it. (Queried Brendan and Raeburn w/full orig context, 26may1993---pesch) - [RHP: I don't understand what the following fragment refers to. If it's the "BIG BUG" section in the original, why does it say "overriding class declarations" here when the more detailed text refers to *function* declarations? Here's the fragment I don't understand:] there are problems with user-supplied overriding class declarations (see below). @end ignore@itemize @bullet@ignore@cindex static data, not working in templates@itemTemplates for static data in template classes do not work.Currently, you must initialize each case of such dataindividually. @c FIXME!! Brendan to see if still true.@c ANSWER: This section presumes that it's incorrect to have to@c initialize for each type you instantiate with. It's not, it's the@c right way to do it.@end ignoreUnfortunately, individual initializations of this sort are likely to beconsidered errors eventually; since they're needed now, you might want toflag places where you use them with comments to mark the need for afuture transition.@cindex nested type results vs templates@itemMember functions in template classes may not have results of nestedtype; @sc{gnu} C++ signals a syntax error on the attempt. The followingexample illustrates this problem with an @code{enum} type @code{alph}:@smallexampletemplate <class T> class list @{ @dots{} enum alph @{a,b,c@}; alph bar(); @dots{}@};template <class T>list<int>::alph list<int>::bar() // @i{Syntax error here}@{@dots{}@}@end smallexample@cindex preprocessor conditionals in templates@cindex conditionals (preprocessor) in templates@itemA parsing bug makes it difficult to use preprocessor conditionals withintemplates. For example, in this code:@smallexampletemplate <class T>class list @{ @dots{}#ifdef SYSWRONG T x;#endif @dots{}@}@end smallexampleThe preprocessor output leaves sourcefile line number information (lineslike @samp{# 6 "foo.cc"} when it expands the @code{#ifdef} block. Theselines confuse the compiler while parsing templates, giving a syntaxerror.If you cannot avoid preprocessor conditionals in templates, you cansuppress the line number information using the @samp{-P} preprocessoroption (but this will make debugging more difficult), by compiling theaffected modules like this:@smallexampleg++ -P foo.cc -o foo@end smallexample@cindex parsing errors, templates@itemParsing errors are reported when templates are first@emph{instantiated}---not on the template definition itself. Inparticular, if you do not instantiate a template definition at all, thecompiler never reports any parsing errors that may be in the templatedefinition.@end itemize@node Template debugging@section Debugging information for templates@cindex templates and debugging information@cindex debugging information and templatesDebugging information for templates works for some object code formats,but not others. It works for stabs@footnote{Except that insufficientdebugging information for methods of template classes is generated instabs.} (used primarily in @sc{a.out} object code, but also in the Solaris 2version of @sc{elf}), and the @sc{mips} version of @sc{coff} debuggingformat.@sc{dwarf} support is currently minimal, and requires furtherdevelopment.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -