C++的复杂度相对于C肯定是复杂很多的,其中表现之一就是C++的名字管理,比如C++支持重载、支持类、命名空间等。不同的函数可以使用相同的函数名、不同的类可以有相同名字的成员变量。这样C++的Name Mangling就会比C复杂很多。
#include<iostream>
using namespace std;
struct A
{
int a ;
template<typename T1, typename T2, typename... Rest>
bool func(int a , int b)
{
T1 t1;
T2 t2;
if(t1.a > a && t2.b > b) return true;
return false;
}
};
struct B
{
int b ;
};
struct C
{
int a ;
};
struct D
{
int b ;
};
template<typename T1, typename T2, typename... Rest>
bool func(int a , int b)
{
T1 t1;
T2 t2;
if(t1.a > a && t2.b > b) return true;
return false;
}
template <typename T1, typename T2>
class my_class : public A
{
public:
void interface(T1 arg1, T2 arg2)
{
cout<<"test"<<arg1.a << arg2.b<<endl;
}
template<typename T4, typename T5>
bool func(int a , int b)
{
T4 t1;
T5 t2;
A::func<T4,T5>(3,4);
if(t1.a > a && t2.b > b) return true;
return false;
}
template<typename T3>
void interface2(T3& arg3)
{
cout<< arg3.b << endl;
A::func<T1,T2>(3,4); //bool A::func<C, D>(int, int) bool A::func<A, B>(int, int)
func<T1,T2>(3,4); //bool my_class<A, B>::func<A, B>(int, int) bool my_class<C, D>::func<C, D>(int, int)
}
void interface3()
{
T1 temp;
T2 temp2;
cout <<"test"<<temp.a << temp2.b<<endl;
}
};
int main()
{
func<A,B>(3,4); //bool func<A, B>(int, int)
func<C,D>(5,6); //bool func<C, D>(int, int)
my_class<A,B> temp1;
my_class<C,D> temp2;
A arg1;
B arg2;
temp1.interface(arg1,arg2); //void my_class<A, B>::interface(A, B)
temp1.interface2(arg2); //void my_class<A, B>::interface2<B>(B&)
temp1.interface3(); //void my_class<A, B>::interface3()
C arg3;
D arg4;
temp2.interface(arg3,arg4); //void my_class<C, D>::interface(C, D)
temp2.interface2(arg4); //void my_class<C, D>::interface2<D>(D&)
temp2.interface3(); //void my_class<C, D>::interface3()
}
如上代码,编译后在命令行输入nm a.out,得到可执行程序的符号。
SENSETIME\sunbaonian@cn3114002715l:~/Learning/Cplusplus/template$ nm a.out
0000000000004010 B __bss_start
0000000000004150 b completed.8061
U __cxa_atexit@@GLIBC_2.2.5
w __cxa_finalize@@GLIBC_2.2.5
0000000000004000 D __data_start
0000000000004000 W data_start
0000000000001130 t deregister_tm_clones
00000000000011a0 t __do_global_dtors_aux
0000000000003d78 d __do_global_dtors_aux_fini_array_entry
0000000000004008 D __dso_handle
0000000000003d80 d _DYNAMIC
0000000000004010 D _edata
0000000000004158 B _end
0000000000001728 T _fini
00000000000011e0 t frame_dummy
0000000000003d68 d __frame_dummy_init_array_entry
0000000000002384 r __FRAME_END__
0000000000003f80 d _GLOBAL_OFFSET_TABLE_
00000000000012f0 t _GLOBAL__sub_I_main
w __gmon_start__
000000000000200c r __GNU_EH_FRAME_HDR
0000000000001000 t _init
0000000000003d78 d __init_array_end
0000000000003d68 d __init_array_start
0000000000002000 R _IO_stdin_used
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000001720 T __libc_csu_fini
00000000000016b0 T __libc_csu_init
U __libc_start_main@@GLIBC_2.2.5
00000000000011e9 T main
0000000000001160 t register_tm_clones
U __stack_chk_fail@@GLIBC_2.4
0000000000001100 T _start
0000000000004010 D __TMC_END__
00000000000012a3 t _Z41__static_initialization_and_destruction_0ii
0000000000001309 W _Z4funcI1A1BJEEbii
0000000000001335 W _Z4funcI1C1DJEEbii
0000000000001630 W _ZN1A4funcI1C1DJEEEbii
00000000000015b6 W _ZN1A4funcIS_1BJEEEbii
00000000000013c4 W _ZN8my_classI1A1BE10interface2IS1_EEvRT_
0000000000001430 W _ZN8my_classI1A1BE10interface3Ev
00000000000015e6 W _ZN8my_classI1A1BE4funcIS0_S1_EEbii
0000000000001362 W _ZN8my_classI1A1BE9interfaceES0_S1_
00000000000014ee W _ZN8my_classI1C1DE10interface2IS1_EEvRT_
000000000000155a W _ZN8my_classI1C1DE10interface3Ev
0000000000001660 W _ZN8my_classI1C1DE4funcIS0_S1_EEbii
000000000000148c W _ZN8my_classI1C1DE9interfaceES0_S1_
U _ZNSolsEi@@GLIBCXX_3.4
U _ZNSolsEPFRSoS_E@@GLIBCXX_3.4
U _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
U _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4
0000000000004040 B _ZSt4cout@@GLIBCXX_3.4
U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4
0000000000002004 r _ZStL19piecewise_construct
0000000000004151 b _ZStL8__ioinit
U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4
将符号输入一个在线的demangling工具就可以得到它的真实面目了。GCC and MSVC C++ Demangler