ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
template <class T> void f(T)    { cout << "1"; }
template <>        void f(int*) { cout << "2"; }   // explicit specialisation
template <class T> void f(T*)   { cout << "3"; }
int main() { int* p = nullptr; f(p); }
  1. A. 2
  2. B. 3
  3. C. 1
  4. D. Compile-time error: ambiguous call

Correct answer: B. 3

Overload resolution looks only at primary templates: f(T*) is more specialised than f(T), so it wins. The explicit specialisation f<int*> belongs to f(T), which was not chosen, so it is never considered.

More OOP with C++ and Java questions

Exams that ask this topic