Oh dear, I read your original post again, and it looks like I grossly misread it.
Taking the example given in your book, suppose we have a function template that performs a generic swap:
Code:
template <typename T>
void swap(T& x, T& y)
{
T t(x);
x = y;
y = t;
}
We could use this to swap two ints:
Code:
int a = 1, b = 2;
swap(a, b);
The compiler looks at the arguments passed and deduces that T in the template must be int. However, we could also specify this explicitly:
Code:
int a = 1, b = 2;
swap<int>(a, b);
Now, regardless of the argument types, the compiler instantiates swap with T as int.
As for advantages: I would think that most of the time, you want to use implicit specialisation. If an ordinary function of the same signature exists, the compiler will choose that one, and it is more likely to be the "most optimised" version compared to the function template. Perhaps one benefit of explicitly providing template arguments is that you can be sure of what types are being used at that point, but I cannot think of a good example of that right now. There is a rather lame example though:
Code:
template <typename T>
T divide(T x, T y)
{
return x / y;
}
Now, we might use it as:
Code:
std::cout << divide(1, 2) << std::endl;
std::cout << divide<double>(1, 2) << std::endl;
In the first example, we get integer division. In the second, despite the arguments being ints, we get floating point division.
EDIT:
Decided to check my terminology, and looks like I got it wrong. Templates are a tricky business
Anyway, here's the deal: template specialisation occurs when you provide the template with arguments. So, swap(a, b) implicitly specialises swap() with the type of a and b, which in my example was int. divide<double> explicitly specialises divide with double. In both cases, there was also template instantiation, as the compiler had to work out the implementation of the function from the template. The FAQ I linked to talks about explicit specialisation that refines the implementation. In such a case, if you happen to specialise the same function template (which is no longer really a template as its template parameters are all specified), no template instantiation occurs.