CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #86

Spot the Conflicting Template Arguments

easyTemplates

📄 Code

Read carefully — what does this print?
1#include <iostream>
2 
3template <typename T>
4T add(T first, T second) {
5 return first + second;
6}
7 
8int main() {
9 std::cout << add(2, 3) << "\n";
10 std::cout << add(2.0, 3.0) << "\n";
11 // std::cout << add(2, 3.5) << "\n"; // does NOT compile: T would be int from 2 and double from 3.5
12 std::cout << add<double>(2, 3.5) << "\n";
13 std::cout << add<int>(2, 3) << "\n";
14 return 0;
15}

🎯 Your Answer