[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The C++ template(1) facility, which effectively allows use of variables for types in declarations, is one of the newest features of the language.
GNU C++ is one of the first compilers to implement many of the template facilities currently defined by the ANSI committee.
Nevertheless, the template implementation is not yet complete. This chapter maps the current limitations of the GNU C++ template implementation.
1.1 Limitations for function and class templates | ||
1.2 Limitations for function templates | ||
1.3 Limitations for class templates | ||
1.4 Debugging information for templates |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These limitations apply to any use of templates (function templates or class templates) with GNU C++:
When you compile code with templates, the template definitions must come first (before the compiler needs to expand them), and template definitions you use must be visible in the current scope.
Templates for static data in template classes do not work. See section Limitations for class templates.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function templates are implemented for the most part. The compiler can correctly determine template parameter values, and will delay instantiation of a function that uses templates until the requisite type information is available.
The following limitations remain:
The specification in Bjarne Stroustrup’s The C++ Programming Language, Second Edition is narrower, and the GNU C++ implementation is now clearly incorrect. With this new specification, a declaration that corresponds to an instantiation of a function template only affects whether conversions are needed to use that version of the function. It should no longer prevent expansion of the template definition.
For example, this code fragment must be treated differently:
template <class X> X min (X& x1, X& x2) { … } int min (int, int); … int i; short s; min (i, s); // should call min(int,int) // derived from template …
f
:
template <class T> class A { public: T x; class Y {}; }; template <class X> int f (A<X>::Y y) { … }
inline
function using templates, the compiler
can only inline the code after the first time you use
that function with whatever particular type signature the template
was instantiated.
Removing this limitation is akin to supporting nested function definitions in GNU C++; the limitation will probably remain until the more general problem of nested functions is solved.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
enum
type alph
:
template <class T> class list { … enum alph {a,b,c}; alph bar(); … }; template <class T> list<int>::alph list<int>::bar() // Syntax error here { … }
template <class T> class list { … #ifdef SYSWRONG T x; #endif … }
The preprocessor output leaves sourcefile line number information (lines
like ‘# 6 "foo.cc"’ when it expands the #ifdef
block. These
lines confuse the compiler while parsing templates, giving a syntax
error.
If you cannot avoid preprocessor conditionals in templates, you can suppress the line number information using the ‘-P’ preprocessor option (but this will make debugging more difficult), by compiling the affected modules like this:
g++ -P foo.cc -o foo
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Debugging information for templates works for some object code formats, but not others. It works for stabs(2) (used primarily in A.OUT object code, but also in the Solaris 2 version of ELF), and the MIPS version of COFF debugging format.
DWARF support is currently minimal, and requires further development.
[Top] | [Contents] | [Index] | [ ? ] |
Class templates are also known as parameterized types.
Except that insufficient debugging information for methods of template classes is generated in stabs.
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated on January 15, 2023 using texi2html 5.0.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ Up ] | Up | Up section | 1.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated on January 15, 2023 using texi2html 5.0.