In the third-party Library source code, we often see this Code:
# Pragma push_macro ("new ")
# Undef new
// Do something with new
......
# Pragma pop_macro ("new ")
It is used to push the macro definition new to the stack and cancel its definition (macro). As a result, the original meaning of new is restored, after the macro definition is used, the new stack is displayed to restore the macro definition.
However, there are still two questions to answer.
1) Will the macro definition name not conflict with the keyword new?
2) What is the function of macro definition new?
Question 1
If the macro definition name is the same as the reserved keyword, the compiler does not prompt an error. Instead, it uses the newly defined macro definition to replace the keyword. The following is an example that defines a macro int. The routine can be compiled smoothly through the link, and the running result is 8, 4, 8, the same as expected.
[Cpp]
// Leletest. cpp: defines the entry point of the console application.
//
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
# Define int double
Void _ tmain (int argc, _ TCHAR * argv [])
{
Int iOne = 1;
Cout <sizeof (iOne) <endl;
# Pragma push_macro ("int ")
# Undef int
Int iTwo = 2;
Cout <sizeof (iTwo) <endl;
# Pragma pop_macro ("int ")
Int iSecond = 2;
Cout <sizeof (iSecond) <endl;
System ("pause ");
}
// Leletest. cpp: defines the entry point of the console application.
//
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
# Define int double
Void _ tmain (int argc, _ TCHAR * argv [])
{
Int iOne = 1;
Cout <sizeof (iOne) <endl;
# Pragma push_macro ("int ")
# Undef int
Int iTwo = 2;
Cout <sizeof (iTwo) <endl;
# Pragma pop_macro ("int ")
Int iSecond = 2;
Cout <sizeof (iSecond) <endl;
System ("pause ");
}
Resolution: The running result is 8, 4, 8.
Macro definition, for example, # define PI 3.1415926 replace all the PI in the program with 3.1415926
# Define int double indicates that all the places where int appears in the program are replaced with double, (int iOne = 1; cout <sizeof (iOne) <endl; equivalent to iOne, which is double type, so the output is 8)
# Pragma push_macro ("int ")
# Undef intint iTwo = 2;
Cout <sizeof (iTwo) <endl;
# Pragma pop_macro ("int ")
The above sentence means to press the macro definition int into the stack and cancel its definition (macro). As a result, the original meaning of the int is restored, that is, int still indicates int. After the macro definition int is used, the stack is displayed. Restoring the macro definition is that int represents double.