Assuming standard headers and using namespace std, what does this C++ code print? template <typename T> T mx(T a, T b) { return a > b ? a : b; } int main() { cout << mx(3, 7) << ” ” << mx<double>(2, 2.5); }
Correct answer C. 7 2.5
Explanation
For mx(3, 7) the compiler deduces T = int and returns 7. In mx(2, 2.5) the template argument is given explicitly, so no deduction is needed and the int 2 is converted to double, returning 2.5. Without , the call would fail because T would be deduced as int from one argument and double from the other.
Answer checked How we write and check questions
Discussion