Listing 8.13 illustrates how explicit specialization works.

Listing 8.13. twoswap.cpp

// twoswap.cpp -- specialization overrides a template

#include

template

void Swap(T &a, T &b);

struct job

{

    char name[40];

    double salary;

    int floor;

};

// explicit specialization

template <> void Swap(job &j1, job &j2);

void Show(job &j);

int main()

{

    using namespace std;

    cout.precision(2);

    cout.setf(ios::fixed, ios::floatfield);

    int i = 10, j = 20;

    cout << "i, j = " << i << ", " << j << ".\n";

    cout << "Using compiler-generated int swapper:\n";

    Swap(i,j);    // generates void Swap(int &, int &)

    cout << "Now i, j = " << i << ", " << j << ".\n";

    job sue = {"Susan Yaffee", 73000.60, 7};

    job sidney = {"Sidney Taffee", 78060.72, 9};

    cout << "Before job swapping:\n";

    Show(sue);

    Show(sidney);

    Swap(sue, sidney); // uses void Swap(job &, job &)

    cout << "After job swapping:\n";

    Show(sue);

    Show(sidney);

    // cin.get();

    return 0;

}

template

void Swap(T &a, T &b)    // general version

{

    T temp;

    temp = a;

    a = b;

    b = temp;

}

// swaps just the salary and floor fields of a job structure

template <> void Swap(job &j1, job &j2)  // specialization

{

    double t1;

    int t2;

    t1 = j1.salary;

    j1.salary = j2.salary;

    j2.salary = t1;

    t2 = j1.floor;

    j1.floor = j2.floor;

    j2.floor = t2;

}

void Show(job &j)

{

    using namespace std;

    cout << j.name << ": $" << j.salary

         << " on floor " << j.floor << endl;

}

Here’s the output of the program in Listing 8.13:

i, j = 10, 20.

Using compiler-generated int swapper:

Now i, j = 20, 10.

Before job swapping:

Susan Yaffee: $73000.60 on floor 7

Sidney Taffee: $78060.72 on floor 9

After job swapping:

Susan Yaffee: $78060.72 on floor 9

Sidney Taffee: $73000.60 on floor 7

Instantiations and Specializations

To extend your understanding of templates, let’s investigate the terms instantiation and specialization. Keep in mind that including a function template in your code does not in itself generate a function definition. It’s merely a plan for generating a function definition. When the compiler uses the template to generate a function definition for a particular type, the result is termed an instantiation of the template. For example, in Listing 8.13, the function call Swap(i,j) causes the compiler to generate an instantiation of Swap(), using int as the type. The template is not a function definition, but the specific instantiation using int is a function definition. This type of instantiation is termed implicit instantiation because the compiler deduces the necessity for making the definition by noting that the program uses a Swap() function with int parameters.

Originally, using implicit instantiation was the only way the compiler generated function definitions from templates, but now C++ allows for explicit instantiation. That means you can instruct the compiler to create a particular instantiation—for example, Swap()—directly. The syntax is to declare the particular variety you want, using the <> notation to indicate the type and prefixing the declaration with the keyword template:

template void Swap(int, int);  // explicit instantiation

A compiler that implements this feature will, upon seeing this declaration, use the Swap() template to generate an instantiation, using the int type. That is, this declaration means “Use the Swap() template to generate a function definition for the int type.” Contrast the explicit instantiation with the explicit specialization, which uses one or the other of these equivalent declarations:

template <> void Swap(int &, int &);  // explicit specialization

template <> void Swap(int &, int &);       // explicit specialization

The difference is that these last two declarations mean “Don’t use the Swap() template to generate a function definition. Instead, use a separate, specialized function definition explicitly defined for the int type.” These prototypes have to be coupled with their own function definitions. The explicit specialization declaration has <> after the keyword template, whereas the explicit instantiation omits the <>.

Caution

It is an error to try to use both an explicit instantiation and an explicit specialization for the same type(s) in the same file, or, more generally, the same translation unit.

Explicit instantiations also can be created by using the function in a program. For instance, consider the following:

template

T Add(T a, T b)    // pass by value

{

    return a + b;

}

...

int m = 6;

double x = 10.2;

cout << Add(x, m) << endl;  // explicit instantiation

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

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

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