by Nikolay Krasilnikov, June 2012 (updated by Petr Kudryavtsev, June 2016)
The Oracle Developer Studio C++ compiler supports two kinds of templates that enable you to write a single body of code which applies to a wide range of types in a type-safe manner.
Template instantiation involves generating a concrete class or function—an instance—for a particular combination of template arguments.
Template specialization allows you to define alternative implementations for a given combination of actual template arguments. The template specialization overrides the default instantiation of the template.
In Oracle Developer Studio, the IDE significantly extends the performance and accuracy of support for C++ templates with new annotations and code completion features.
In the IDE, annotation icons are displayed in the left margin of the Source Editor window.
Two new annotations were added in the Oracle Developer Studio IDE that simplify navigation between generic template instantiations and template specializations. The icons for these annotations are:
Figure 1 shows an example specialization of the
simpleVector class template. When the user right-clicked the specializes icon on line 23, the IDE displayed the annotation for the specialization. If the user clicked the annotation, the IDE would display the main template for the class.
Figure 1. Source Code Listing in the Source Editor Showing a Specialized Annotation
The example in Figure 2 shows the specializes icons for specializations of a function template.
Figure 2. Source Code Listing in the Source Editor Showing Several Specializations of a Function Template
The function declaration elsewhere in the code is marked with a "has specialization" icon. If the user clicked that icon, the IDE would display the annotation, which includes a list of the specializations from which the user could choose. Selecting a specialization would cause the IDE to display that specialization.
Figure 3. Source Code Listing Showing a "Has Specialization" Annotation
The Oracle Developer Studio IDE includes new code completion for C++ templates, which instantiates and specializes template constructions.
Figure 4. Source Code Listing Showing Code Completion for a C++ Template
Code completion substitutes
. if you make an error.
The Oracle Developer Developer IDE can calculate some metaprogramming expressions in templates, for example:
Figure 5. Source Code Listing in the Source Editor Showing Warning Icons on Odd Instantiations
For this template, the code model understands that the
i field should be only in even instantiations. So the IDE marks the odd instantiations with warning icons.
The same is true for static variables.
Figure 6. Source Code Listing in the Source Editor Showing Warning Icon for Static Variable in a Template Instantiation
Figure 7. Range-based loops and auto keyword
Figure 8. Range-based loops and auto keyword
Figure 9. Range-based loops and auto keyword
See the following resources for more information:
Nikolay Krasilnikov joined Sun Microsystems in 2008. Now he is a senior software engineer for Oracle in St. Petersburg, Russia developing the Oracle Developer Studio IDE. His responsibilities include developing the C and C++ grammar, parser, and code model.
|Revision 1.1, June 2016|
|Revision 1.0, June 2012|