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::first()

{

    return a;

}

template

T2 & Pair::second()

{

    return b;

}

int main()

{

    using std::cout;

    using std::endl;

    using std::string;

    Pair ratings[4] =

    {

        Pair("The Purpled Duck", 5),

        Pair("Jaquie's Frisco Al Fresco", 4),

        Pair("Cafe Souffle", 5),

        Pair("Bertie's Eats", 3)

    };

    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 to invoke the constructors and as an argument for sizeof. That’s because Pair and not Pair is the class name. Also Pair would be the name of an entirely different class.

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 class Topo {...};

This causes the compiler to use int for the type T2 if a value for T2 is omitted:

Topo m1; // T1 is double, T2 is double

Topo m2;         // T1 is double, T2 is int

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 specializations. That is, a template describes a class in terms of a general type, whereas a specialization is a class declaration generated by using a specific type.

Implicit Instantiations

The template examples you have seen so far in this chapter use implicit instantiations. That is, they declare one or more objects indicating the desired type, and the compiler generates a specialized class definition, using the recipe provided by the general template:

ArrayTP stuff; // implicit instantiation

The compiler doesn’t generate an implicit instantiation of the class until it needs an object:

ArrayTP * pt;     // a pointer, no object needed yet

pt = new ArrayTP; // now an object is needed

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 explicit instantiation of a class declaration when you declare a class by using the keyword template and indicating the desired type or types. The declaration should be in the same namespace as the template definition. For example, the following declaration declares ArrayTP to be a class:

template class ArrayTP; // generate ArrayTP class

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

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги