ProgrammingVery hardGATE CS · TCS Digital · Advanced

What is printed? (C++17, assume <iostream> and using namespace std)

What is printed? (C++17, assume <iostream> and using namespace std)
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 = 0;
    f(x);
    f(&x);
    f<int>(x);
    f('a');
    f(0L);
}
  1. A. IPTTT
  2. B. IPITT
  3. C. IPIIT
  4. D. TPTTT

Correct answer: A. IPTTT

f(x): non-template f(int) ties the template on conversions and wins. f(&x): f(T*) is more specialised than f(T). f<int>(x) names a template, so f(int) is excluded. 'a' and 0L match f(T) exactly, beating the promotion/conversion needed for f(int).

More OOP with C++ and Java questions

Exams that ask this topic