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

by Nikolay Krasilnikov, June 2012 (updated by Petr Kudryavtsev, June 2016)

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

New Annotations for Template Specializations
Code Completion for C++ Templates
Calculations on Templates
Support of C++11 and some C++14 features
See Also
About the Author

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.

  • 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 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:

  • 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 Developer Studio 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.

Calculations on Templates


The Oracle Developer Developer 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

Support of C++11 and some C++14 features


Figure 7

Figure 7. Range-based loops and auto keyword


Figure 8

Figure 8. Range-based loops and auto keyword


Figure 9

Figure 9. Range-based loops and auto keyword


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 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

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