← Back to Templates
Question 746
Add Mixed Numeric Types with auto
Add Mixed Numeric Types with auto
Write a function template addMixed(T first, U second) with two type parameters and an auto return type. It returns first + second.
With auto, the compiler takes the return type from the addition itself: int + int is an int, int + double is a double and double + int is a double, so a decimal result is never cut off. The tests print addMixed(2, 3), addMixed(2, 0.5) and addMixed(1.25, 4) on separate lines. Work out the deduced return type of each call before you run it.
Do NOT write a main function.
Expected Output:
5 2.5 5.25
= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
Templates
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.