#include <iostream> using namespace std; template <typename T> T max(T x, T y) { return (x > y)? x : y; } int main() { cout << max(3, 7) << std::endl; cout << max(3.0, 7.0) << std::endl; cout << max(3, 7.0) << std::endl; return 0; } |
chevron_right
filter_none
(A)
7 7.0 7.0
(B) Compiler Error in all cout statements as data type is not specified.
(C) Compiler Error in last cout statement as call to max is ambiguous.
(D) None of the above
Answer: (C)
Explanation: The first and second call to max function is a valid call as both the arguments passed are of same data type (i.e int and float respectively). But the third call to max function has arguments of different data type and hence it will generate Compiler Error in last cout statement as call to max is ambiguous. Hence option C is correct
Quiz of this Question
Recommended Posts:
- C++ | Templates | Question 1
- C++ | Templates | Question 2
- C++ | Templates | Question 4
- C++ | Templates | Question 5
- C++ | Templates | Question 6
- C++ | Templates | Question 10
- C++ | Templates | Question 8
- C++ | Templates | Question 9
- 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

