質問

I have the following code:

//1
template<typename T>
void c(T in) {
    cout << "Template c(" << in << ")" << endl;
}
//2
template<>
void c<>(int* in) { 
        cout << "Template specialization b(" << in << ")" <<endl;
}
//3
template<typename T>
void c(T* in) {
        cout << "Template for pointers c(" << in << ")" <<endl;
}
//..
int i = 8;
c(&i);

Can someone explain me why in the following example compiler choose function #3, but when I change the order of functions #2 and #3, then compiler choose function #2?

役に立ちましたか?

解決

The compiler first chooses the primary template and only then determines which specialization to use. That is, in your case the compiler always chooses the second primary template, i.e., #3.

However, since you didn't specify the template argument when specializing the function template, your specialization specializes a different primary template depending on its location: with the given order, it specializes the first primary template, when you exchange the order of #2 and #3 it specializes the the second primary template. In 14.7.3 [temp.expl.spec] paragraph 7 the standard has to say the following about the situation

... When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

If you wanted to control which primary template the specialization actually specializes, you would specify the template arguments in the specialization:

template <> void c<int*>(int* in) { ... } // specializes the first primary
template <> void c<int>(int* in)  { ... } // specializes the second primary
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top