ProgrammingVery hardProduct-company placement · GATE CS

What is printed?

What is printed?
template <class T> void f(T)  { cout << "T"; }
template <class T> void f(T*) { cout << "P"; }
void f(int) { cout << "I"; }
int main() {
    int x = 1;
    f(x); f(&x); f(1.0); f<>(x);
}
  1. A. IPTT
  2. B. TPTT
  3. C. ITTT
  4. D. IPIT

Correct answer: A. IPTT

f(x): the non-template and f<int> are equally good, and a non-template wins ties. f(&x): both templates match exactly, and partial ordering prefers the more specialised f(T*). f(1.0): f<double> is exact, better than converting to int. f<>(x) allows only templates.

More OOP with C++ and Java questions

Exams that ask this topic