Question

1.
2.
3.
4.
Answer (Detailed Solution Below)

Option 2 : I / I

Advertisement

Detailed Solution

सही उत्तर I / I है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is the output of the following C++ code? `template<typename T> void f(T x){ cout<<"T"; } void f(int x){ cout<<"I"; } int main(){ f(5); }` / निम्नलिखित C++ कोड का आउटपुट क्या होगा? `template<typename T> void f(T x){ cout<<"T"; } void f(int x){ cout<<"I"; } int main(){ f(5); }`

1.
T / T
2.
I / I
3.
Compilation error — ambiguous call / कंपाइलेशन त्रुटि — अस्पष्ट कॉल
4.
TI / TI

Correct Answer: I / I

When a non-template overload and a template specialization match equally well, overload resolution prefers the non-template function, so `f(int)` is chosen and prints I. The trap is assuming the template is preferred or that the call is ambiguous. / जब non-template overload और template specialization समान रूप से मेल खाते हैं, तो overload resolution non-template function को प्राथमिकता देता है, अतः `f(int)` चुना जाता है और I छपता है। जाल यह मानना है कि template को प्राथमिकता मिलती है या कॉल अस्पष्ट है।

Downloaded from PrepSnap (prepsnap.in)