在C++中, 是允许函数重载的, 也就引出了编译器的name mangling机制, c++filt命令便与此有关。由于每一个重载的函数都使用与原函数相同的名称,因此,支持函数重载的语言必须拥有一种机制,以区分同一个函数的许多重载版本。下面是C++代码
#include <iostream>
void Swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void Swap(float *a, float *b)
{
float temp = *a;
*a = *b;
*b = temp;
}
void Swap(char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
void Swap(bool *a, bool *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int n1 = 100, n2 = 200;
Swap(&n1, &n2);
float f1 = 12.5, f2 = 56.93;
Swap(&f1, &f2);
char c1 = 'A', c2 = 'B';
Swap(&c1, &c2);
bool b1 = false, b2 = true;
Swap(&b1, &b2);
return 0;
}
编译
g++ -o test4 test1.cpp
查看重载函数的符号,这里并不能正常识别区分函数重载的各个函数
nm cpp_test | grep demo
使用c++filt可以很好的识别出函数重载的问题
nm test4 | grep Swap |c++filt