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 #129

The Error That Waits for Instantiation

hardTemplates

Without the larger(p, q) line the program compiles and prints 9. Adding that line makes the build fail. Why?

📄 Code

1#include <iostream>
2 
3template <typename T>
4T larger(T a, T b) {
5 if (a < b) {
6 return b;
7 }
8 return a;
9}
10 
11struct Point {
12 int x;
13 int y;
14};
15 
16int main() {
17 std::cout << larger(3, 9) << "\n";
18 Point p{1, 2};
19 Point q{3, 4};
20 Point r = larger(p, q); // this line breaks the build
21 std::cout << r.x << "\n";
22 return 0;
23}