How to Use C++ Templates in the Oracle Solaris Studio IDE

by Nikolay Krasilnikov

This article describes features in the Oracle Solaris Studio 12.3 Integrated Development Environment (IDE) that provide enhanced support for C++ templates.


Published July 2012

New Annotations for Template Specializations
Code Completion for C++ Templates
Experimental Features for Templates
See Also
About the Author

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.

OTN is all about helping you become familiar enough with Oracle technologies to make an informed decision. Articles, software downloads, documentation, and more. Join up and get the technical resources you need to do your job.
  • A function template describes a set of related functions that differ only by the types of their arguments or return values. You can use a function template like any other function.
  • A class template describes a set of related classes or data types that differ only by types, by integral values, by pointers or references to variables with global linkage, or by a combination thereof. You can use a class template wherever you can use a type.

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.

New Annotations for Template Specializations

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:

  • has specialization icon1: has specialization
  • specialized icon: specializes

Class Template Specialization

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

Figure 1. Source Code Listing in the Source Editor Showing a Specialized Annotation

Function Template Specialization

The example in Figure 2 shows the specializes icons for specializations of a function template.

Figure 2

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

Figure 3. Source Code Listing Showing a "Has Specialization" Annotation

Code Completion for C++ Templates

The Oracle Solaris Studio 12.3 IDE includes new code completion for C++ templates, which instantiates and specializes template constructions.

Figure 4

Figure 4. Source Code Listing Showing Code Completion for a C++ Template

Code completion substitutes -> for . if you make an error.

Experimental Features for Templates

Some experimental support for C++ templates is available in the IDE, but it is disabled by default.

Enabling Experimental Features

You can enable these experimental features by including the following options when you start the IDE with the solstudio command:

  • -J-cnd.modelimpl.expression.evaluator.deep.variable.provider=true
  • -J-cnd.modelimpl.expression.evaluator.recursive.calc=true
  • -J-cnd.modelimpl.expression.evaluator.extra.spec.params.matching=true

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.

Calculations on Templates

The Oracle Solaris Studio 12.3 IDE can calculate some metaprogramming expressions in templates, for example:

Figure 5

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

Figure 6. Source Code Listing in the Source Editor Showing Warning Icon for Static Variable in a Template Instantiation

See Also

See the following resources for more information:

About the Author

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

See sysadmin-related content for all Oracle technologies by following OTN Systems on Facebook and Twitter.