← Back to Question List
C++ Quiz #126
Why swapValues(x, 5) Will Not Compile
swapValues(x, y) works and prints 2 1, but the program fails to compile because of swapValues(x, 5). Why?
📄 Code
1#include <iostream>23template <typename T>4void swapValues(T& a, T& b) {5 T temp = a;6 a = b;7 b = temp;8}910int 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}