Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.