Skip to content
Related Articles

Related Articles

Improve Article

C++ | Templates | Question 9

  • Last Updated : 28 Jun, 2021

Output?




#include <iostream>
using namespace std;
  
template <class T>
T max (T &a, T &b)
{
    return (a > b)? a : b;
}
  
template <>
int max <int> (int &a, int &b)
{
    cout << "Called ";
    return (a > b)? a : b;
}
  
int main ()
{
    int a = 10, b = 20;
    cout << max <int> (a, b);
}

(A) 20
(B) Called 20
(C) Compiler Error


Answer: (B)

Explanation: Above program is an example of template specialization. Sometime we want a different behaviour of a function/class template for a particular data type. For this, we can create a specialized version for that particular data type.

Quiz of this Question

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :