The template would fail to match the function call Add(x, m) because the template expects both function arguments to be of the same type. But using Add(x, m) forces the type double instantiation, and the argument m is type cast to type double to match the second parameter of the Add(double, double) function.

What if you do something similar with Swap()?

int m = 5;

double x  = 14.3;

Swap(m, x);  // almost works

This generates an explicit instantiation for type double. Unfortunately, in this case, the code won’t work because the first formal parameter, being type double &, can’t refer to the type int variable m.

Implicit instantiations, explicit instantiations, and explicit specializations collectively are termed specializations. What they all have in common is that they represent a function definition that uses specific types rather than one that is a generic description.

The addition of the explicit instantiation led to the new syntax of using template and template <> prefixes in declarations to distinguish between the explicit instantiation and the explicit specialization. As in many other cases, the cost of doing more is more syntax rules. The following fragment summarizes these concepts:

...

template

void Swap (T &, T &);  // template prototype

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

int main(void)

{

  template void Swap(char &, char &); // explicit instantiation for char

  short a, b;

  ...

  Swap(a,b);    // implicit template instantiation for short

  job n, m;

  ...

  Swap(n, m);   // use explicit specialization for job

  char g, h;

  ...

  Swap(g, h);  // use explicit template instantiation for char

  ...

}

When the compiler reaches the explicit instantiation for char, it uses the template definition to generate a char version of Swap(). For the remaining uses of Swap(), the compiler matches a template to the actual arguments used in the function call. For example, when the compiler reaches the function call Swap(a,b), it generates a short version of Swap() because the two arguments are type short. When the compiler reaches Swap(n,m), it uses the separate definition (the explicit specialization) provided for the job type. When the compiler reaches Swap(g,h), it uses the template specialization it already generated when it processed the explicit instantiation.

Which Function Version Does the Compiler Pick?

What with function overloading, function templates, and function template overloading, C++ needs, and has, a well-defined strategy for deciding which function definition to use for a function call, particularly when there are multiple arguments. The process is called overload resolution. Detailing the complete strategy would take a small chapter, so let’s take just a broad look at how the process works:

Phase 1— Assemble a list of candidate functions. These are functions and template functions that have the same names as the called functions.

Phase 2— From the candidate functions, assemble a list of viable functions. These are functions with the correct number of arguments and for which there is an implicit conversion sequence, which includes the case of an exact match for each type of actual argument to the type of the corresponding formal argument. For example, a function call with a type float argument could have that value converted to a double to match a type double formal parameter, and a template could generate an instantiation for float.

Phase 3— Determine whether there is a best viable function. If so, you use that function. Otherwise, the function call is an error.

Consider a case with just one function argument—for example, the following call:

may('B');    // actual argument is type char

First, the compiler rounds up the suspects, which are functions and function templates that have the name may(). Then, it finds those that can be called with one argument. For example, the following pass muster because they have the same name and can be used with one argument:

void may(int);                            // #1

float may(float, float = 3);              // #2

void may(char);                           // #3

char * may(const char *);                 // #4

char may(const char &);                   // #5

template void may(const T &);    // #6

template void may(T *);          // #7

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

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

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