You can have a template with more than one type parameter. For example, suppose you want a class that holds two kinds of values. You can create and use a Pair template class for holding two disparate values. (Incidentally, the STL provides a similar template called pair.) The short program in Listing 14.19 shows an example. In it, the first() const and second() const methods report the stored values, and the first() and second() methods, by virtue of returning references to the Pair data members, allow you to reset the stored values by using assignment.
Listing 14.19. pairs.cpp
// pairs.cpp -- defining and using a Pair template
#include
#include
template
class Pair
{
private:
T1 a;
T2 b;
public:
T1 & first();
T2 & second();
T1 first() const { return a; }
T2 second() const { return b; }
Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) { }
Pair() {}
};
template
T1 & Pair
{
return a;
}
template
T2 & Pair
{
return b;
}
int main()
{
using std::cout;
using std::endl;
using std::string;
Pair
{
Pair
Pair
Pair
Pair
};
int joints = sizeof(ratings) / sizeof (Pair
cout << "Rating:\t Eatery\n";
for (int i = 0; i < joints; i++)
cout << ratings[i].second() << ":\t "
<< ratings[i].first() << endl;
cout << "Oops! Revised rating:\n";
ratings[3].first() = "Bertie's Fab Eats";
ratings[3].second() = 6;
cout << ratings[3].second() << ":\t "
<< ratings[3].first() << endl;
return 0;
}
One thing to note about Listing 14.19 is that in main(), you have to use Pair
Here’s the output of the program in Listing 14.19:
Rating: Eatery
5: The Purpled Duck
4: Jaquie's Frisco Al Fresco
5: Cafe Souffle
3: Bertie's Eats
Oops! Revised rating:
6: Bertie's Fab Eats
Default Type Template Parameters
Another new class template feature is that you can provide default values for type parameters:
template
This causes the compiler to use int for the type T2 if a value for T2 is omitted:
Topo
Topo
The STL (discussed in Chapter 16) often uses this feature, with the default type being a class.
Although you can provide default values for class template type parameters, you can’t do so for function template parameters. However, you can provide default values for non-type parameters for both class and function templates.
Template Specializations
Class templates are like function templates in that you can have implicit instantiations, explicit instantiations, and explicit specializations, collectively known as
Implicit Instantiations
The template examples you have seen so far in this chapter use
ArrayTP
The compiler doesn’t generate an implicit instantiation of the class until it needs an object:
ArrayTP
pt = new ArrayTP
The second statement causes the compiler to generate a class definition and also an object that is created according to that definition.
Explicit Instantiations
The compiler generates an
template class ArrayTP
In this case, the compiler generates the class definition, including method definitions, even though no object of the class has yet been created or mentioned. Just as with the implicit instantiation, the general template is used as a guide to generate the specialization.
Explicit Specializations