Eduard Rostomyan
Eduard Rostomyan

Reputation: 6546

Why not specialize function templates? (Q&A)

I will try to organize this problem alas Q&A and hope that there will be colleagues who will take something from this.

First, lets answer at first glance a simple question, what is the output of this program?

#include <iostream>

template<class T>
void f(T) { std::cout << 1; }

template<>
void f<>(int*) { std::cout << 2; }

template<class T>
void f(T*) { std::cout << 3; }

int main() {
    int *p = nullptr; 
    f( p );
}

Upvotes: 2

Views: 399

Answers (1)

Eduard Rostomyan
Eduard Rostomyan

Reputation: 6546

If your answer was 3, congratulations, you are free to skip this Q&A.

For everybody else:

I personally thought that the output should be 2, if you got to this point, I guess you too.
Now lets dive a bit deeper.

The name f is overloaded by the two function templates void f(T) and void f(T*). Note that overload resolution only considers the function templates, not the explicit specialisation void f<>(int*)! For overload resolution, first we deduce the template arguments for each function template, and get T = int * for the first one, and T = int for the second.

Both function templates are viable, but which one is best? According to [over.match.best]§16.3.3¶1, a function template is a better match than another function template if it's more specialised:

a viable function F1 is defined to be a better function than another viable function F2 if (...) the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 17.5.6.2

The process of partial ordering is a bit long to quote here, and not key to this question. But in summary, anything accepted by f(T*) would also be accepted by f(T), but not the other way around. So f(T*) is more specialised.

Now we know that the function template void f(T*) is selected by overload resolution, and we can start thinking about specialisations. Which of the function templates is void f<>(int*) a specialisation of? [temp.expl.spec]§17.7.3¶3:

A declaration of a function template (...) being explicitly specialized shall precede the declaration of the explicit specialization.

So the explicit specialisation void f<>(int*) is a specialisation of void f(T), since that's the only function template declaration that precedes it. Overload resolution however selected the other function template, void f(T*), and we instead call an implicitly instantiated a specialisation of that, printing 3.

For more, please refer to Herb Sutter's article

Upvotes: 4

Related Questions