We know that in C + + there is a function overload, when we define a few functions similar to the function name is the same function, as long as its argument list is different, the compilation can be passed, but in C is not possible.
double Add (doubledouble b) { return a + b;} int Add (intchar b) { return a + b;} Char Add (charchar b) { return a + b;}
If this code is written in C, it will be errorC2371, which is called the Add function redefinition. But if this is allowed in the C + + environment, called a function overload, as long as you have several functions that meet the requirements of function overloading, there is no problem at all. This leads to the modification of the function name in C and C + +, why is it not in C?
Double Add (double A, double b);
//{
return a + B;
//}
I change the definition of this function to a declaration and then call it in the main function to see the
1. C compiler's function name decoration rules
Compiler gave us a link error, you can see _add such a function, this is the C environment in the compiler to our function name decoration, you can see the function is preceded by an underscore, of course, this modification is the default calling convention __cdecl case, if the __ StdCall calling convention, the compiler and linker will precede the output function name with an underscore prefix, the function name followed by an "@" symbol and the number of bytes of its argument, the __fastcall calling convention to precede the output function name with an "@" symbol, followed by an "@" symbol and the number of bytes of its parameters.
We can modify the calling convention to see if this is the case.
After this modification, you can see that the Add function is decorated as [email protected], so it is different for the rules to be decorated under various calling conventions.
1. The C + + compiler's function name decoration rules
Then, let's look at how the compiler modifies our functions in C + + environments:
As you can see, for the Double add (double a,double b) function I declare, it is modified to ([email protected]@[email protected]) and you can see that this is completely different from the C environment, So this is also in C + +
There is a reason for the function overloading, so let's see how this is actually decorated.
c++ 's function name decoration rules are somewhat complex, but the information is fuller, and by parsing the decorated names you can not only know how the function is called, Returns the value type, the number of arguments, or even the parameter type. Regardless of the __cdecl,__fastcall or __stdcall invocation, the function decoration takes a "?" Start, followed by the name of the function followed by the start of the parameter table and the parameter list by the parameter type code. For __stdcall mode, the start identifier of the parameter table is "@ @YG", the __cdecl method is "@ @YA", and the __fastcall method is "@ @YI". The spelling codes for the parameter table are as follows:
x--void
d--char
e--unsigned char
f--short
h--int
i--unsigned int
j--long
k--unsigned Long (DWORD)
m--float
n--double
_n--bool
U--struct
you can see that n is a type of double, then ([email protected]@[email protected]) can understand, function modifier? , followed by the function name, then the start identifier of the _CDECL convention @ @YA followed by the return value type, the parameter list parameter type is exactly, then double add (double a,double b) is nnn, after the parameter table with "@z" to identify the end of the entire name, If the function has no arguments, it ends with the "Z" identifier.
And when the argument list has pointers, the pointers are a bit more specific, with PA for pointers, and PB for const type pointers. The following code indicates the pointer type, and if a pointer of the same type appears consecutively, substituting "0", a "0" represents a repetition.
Give a simple example: int fun (int *p1, int *p2);
As you can see, my fun function is modified ([email protected]@[email protected]).
There is also a corresponding representation for the adornment of public and private member functions in C + + member functions. All in all, in C + + environment, when the function name is decorated with the parameter list information, and the return value of information, so the function overload in C + + is allowed to exist, because it can be based on your parameter list to choose the corresponding function, and obviously in our C environment is not allowed.
The C + + compiler's function name decoration rules