← Back to Templates
Question 751
Trailing Return Type with decltype
Trailing Return Type with decltype
Write a function template add(T first, U second) whose return type is written AFTER the parameter list with the trailing-return syntax:
auto add(T first, U second) -> decltype(first + second)Here auto is only a placeholder; decltype(first + second) tells the compiler to use whatever type the addition produces, and it may name the parameters because they are declared to its left. The tests print add(2, 3), add(2, 2.5), add(1.5, 2) and add(std::string("tem"), std::string("plate")) on separate lines.
Do NOT write a main function.
Expected Output:
5 4.5 3.5 template
= 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.