Such overloading should be allowed. The following is the test code (it can also work normally before adding template)
#include <cstdio>
template<typename T> void swap(T *a, T *b)
{ T t = *a; *a = *b; *b = t; }
template<typename T> void swap(T &a, T &b)
{ T t = a; a = b; b = t; }
int main()
{
int x = 3, y = 6;
printf("%d %d\n", x, y);
swap<int>(&x, &y);
printf("%d %d\n", x, y);
swap<int>(x, y);
printf("%d %d\n", x, y);
return 0;
}
Is this what the question means? Also, "the compiler will report an error when calling a reference function" does this mean that the compiler reports an error? Can you give an error message?
Thank you
EDIT:
I see the following content in your error message:
..../type_traits:3201:1: note: candidate function [with _Tp = int]
The function name should be duplicated with the swap function definition in the type_traits file (maybe the compiler automatically includes it), resulting in an error. You can try changing the name of the swap function in the test code to resolve the conflict.
PS The compilation command I used: g++ -Wall -o test test.cpp
Such overloading should be allowed. The following is the test code (it can also work normally before adding template)
Is this what the question means? Also, "the compiler will report an error when calling a reference function" does this mean that the compiler reports an error? Can you give an error message?
Thank you
EDIT:
I see the following content in your error message:
The function name should be duplicated with the swap function definition in the type_traits file (maybe the compiler automatically includes it), resulting in an error. You can try changing the name of the swap function in the test code to resolve the conflict.
PS The compilation command I used: g++ -Wall -o test test.cpp