Friday, October 28, 2011

Class Templates in C++

In one of my previous posts, I made some short notes about class and function nesting. Now, I'd like to discuss more about class templates in C++.

First, we need be able to tell the difference between class template and template class.

Class template: is a template used to generate template classes. You cannot declare an object of a class template.
Template class: is an instance of a class template.

The class template definition is preceded by
    template< template-parameter-list >
where template-parameter-list is a comma-separated list of one or more of the following kinds of template parameters:
    type, non-type, template
Here is an example for template type for template parameter:
    template<template <class T> class X> class A { };
    template<class T> class B { };
    A<b> a;
Multiple template declarations may exist and all template declarations for a class template must have the same types and number of template arguments. Only one template declaration containing the class definition is allowed.

When nested template argument lists are needed, a separating space between the > at the end of the inner list and the > at the end of the outer list is necessary. Otherwise, there is an ambiguity between the extraction operator >> and two template list delimiters >. This is an example:
     map< int, vector<int> >
     map<int, vector<int>>      // error
I double checked this and found that it is an error with g++ but okay with MSVC 2010.
     map<int, vector<int>>      // okay with MSVC 2010
Templates can be defined within ordinary classes or class templates, in which case they are referred to as member templates. Member templates that are classes are referred to as nested class templates.

Nested class templates are declared as class templates inside the scope of the outer class. They can be defined inside or outside of the enclosing class.

When nested class templates are defined outside of their enclosing class, they must be prefaced by the template parameters for both the class template (if they are members of a class template) and template parameters for the member template.

One template parameter list is required for each level, and cannot combine multiple lists.
template<outer-template-parameter-list>
template<inner-template-parameter-list>
return_type Outer<..>::Inner<..>::func(..) {}

// below is illegal
template<outer-template-parameter-list, inner-template-parameter-list>
return_type Outer<..>::Inner<..>::func(..) {}

// if outer class is ordinary class, then only one template list is possible
template<inner-template-parameter-list>
return_type Outer::Inner<..>::func(..) {} 

It is not allowed to specialize template members of a template class without specializing the outside class.

Update:
Local classes are not allowed to have member templates.

References:
IBM publib
Microsoft MSDN

Related Posts:
Nested Classes and Functions
Function Template Specialization

No comments:

Post a Comment