← Back to Question List
C++ Quiz #129
The Error That Waits for Instantiation
Without the larger(p, q) line the program compiles and prints 9. Adding that line makes the build fail. Why?
📄 Code
1#include <iostream>23template <typename T>4T larger(T a, T b) {5 if (a < b) {6 return b;7 }8 return a;9}1011struct Point {12 int x;13 int y;14};1516int 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 build21 std::cout << r.x << "\n";22 return 0;23}