by Nikolay Krasilnikov
Published July 2012New Annotations for Template Specializations
The Oracle Solaris 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 Solaris Studio 12.3, 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 Solaris Studio 12.3 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 Solaris Studio 12.3 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.
Some experimental support for C++ templates is available in the IDE, but it is disabled by default.
You can enable these experimental features by including the following options when you start the IDE with the
Enabling these experimental features decreases code model performance in the IDE. However, code model performance was significantly increased in the Oracle Solaris Studio 12.3 IDE, so even with the experimental features enabled, performance should be at the same level as in the Oracle Solaris Studio 12.2 IDE.
The Oracle Solaris Studio 12.3 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
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 Solaris Studio IDE. His responsibilities include developing the C and C++ grammar, parser, and code model.
|Revision 1.0, 07/24/2012|