Skip Navigation Links | |
Exit Print View | |
Oracle Solaris Studio 12.3: C++ User's Guide Oracle Solaris Studio 12.3 Information Library |
3. Using the C++ Compiler Options
6. Creating and Using Templates
6.2.1 Class Template Declaration
6.2.2 Class Template Definition
6.2.3 Class Template Member Definitions
6.2.3.1 Function Member Definitions
6.2.3.2 Static Data Member Definitions
6.3.1 Implicit Template Instantiation
6.3.2 Explicit Template Instantiation
6.3.2.1 Explicit Instantiation of Template Functions
6.3.2.2 Explicit Instantiation of Template Classes
6.3.2.3 Explicit Instantiation of Template Class Function Members
6.3.2.4 Explicit Instantiation of Template Class Static Data Members
6.5 Default Template Parameters
6.6.1 Template Specialization Declaration
6.6.2 Template Specialization Definition
6.6.3 Template Specialization Use and Instantiation
6.7.1 Nonlocal Name Resolution and Instantiation
6.7.2 Local Types as Template Arguments
6.7.3 Friend Declarations of Template Functions
6.7.4 Using Qualified Names Within Template Definitions
6.7.6 Referencing Static Variables and Static Functions
6.7.7 Building Multiple Programs Using Templates in the Same Directory
9. Improving Program Performance
10. Building Multithreaded Programs
12. Using the C++ Standard Library
A function template describes a set of related functions that differ only by the types of their arguments or return values.
You must declare a template before you can use it. A declaration, as in the following example, provides enough information to use the template but not enough information to implement the template.
template <class Number> Number twice( Number original );
In this example, Number is a template parameter; it specifies the range of functions that the template describes. More specifically, Number is a template type parameter, and its use within the template definition stands for a type determined at the location where the template is used.
If you declare a template, you must also define it. A definition provides enough information to implement the template. The following example defines the template declared in the previous example.
template <class Number> Number twice( Number original ) { return original + original; }
Because template definitions often appear in header files, a template definition might be repeated in several compilation units. All definitions, however, must be the same. This restriction is called the One-Definition Rule.
Once declared, templates can be used like any other function. Their use consists of naming the template and providing function arguments. The compiler can infer the template type arguments from the function argument types. For example, you can use the previously declared template as follows:
double twicedouble( double item ) { return twice( item ); }
If a template argument cannot be inferred from the function argument types, it must be supplied where the function is called. For example:
template<class T> T func(); // no function arguments int k = func<int>(); // template argument supplied explicitly