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); } |
chevron_right
filter_none
(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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C++ | Templates | Question 1
- C++ | Templates | Question 2
- C++ | Templates | Question 3
- C++ | Templates | Question 4
- C++ | Templates | Question 5
- C++ | Templates | Question 6
- C++ | Templates | Question 10
- C++ | Templates | Question 8
- C++ | Templates | Question 10
- C++ | Constructors | Question 1
- C++ | References | Question 1
- C++ | References | Question 6
- C++ | References | Question 6
- C++ | Constructors | Question 2
- C++ | Class and Object | Question 3
- C++ | Class and Object | Question 2
- C++ | Constructors | Question 3
- C++ | Constructors | Question 4
- C++ | Constructors | Question 5
- C++ | Misc C++ | Question 1

