CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #120

Trailing Return Type With decltype

mediumTemplates

The return type is written after the arrow: -> decltype(a + b). What does that part do?

📄 Code

1#include <iostream>
2 
3template <typename T, typename U>
4auto add(T a, U b) -> decltype(a + b) {
5 return a + b;
6}
7 
8int main() {
9 std::cout << add(2, 2.5) << "\n";
10 std::cout << add(3, 4) << "\n";
11 return 0;
12}