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

Why swapValues(x, 5) Will Not Compile

mediumTemplates

swapValues(x, y) works and prints 2 1, but the program fails to compile because of swapValues(x, 5). Why?

📄 Code

1#include <iostream>
2 
3template <typename T>
4void swapValues(T& a, T& b) {
5 T temp = a;
6 a = b;
7 b = temp;
8}
9 
10int main() {
11 int x = 1;
12 int y = 2;
13 swapValues(x, y);
14 std::cout << x << " " << y << "\n";
15 swapValues(x, 5);
16 return 0;
17}