ProgrammingVery hardMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
template <class T> void f(T)  { cout << "T"; }
template <> void f<int*>(int*) { cout << "S"; }
template <class T> void f(T*) { cout << "P"; }
int main() {
    int x = 0;
    f(&x);
}
  1. A. S
  2. B. P
  3. C. T
  4. D. Compile error: ambiguous call

Correct answer: B. P

The explicit specialisation belongs to the first template f(T), declared before f(T*) existed. Overload resolution considers only primary templates and picks the more specialised f(T*), so the specialisation is never used.

More OOP with C++ and Java questions

Exams that ask this topic