Summary of the macro scale

Source: Internet
Author: User

In C ++, although using macros is not recommended, macros often provide us with some convenience, and there are indeed a large number of Macros in the existing code. Some people think that macros are the root cause of errors, because some strange code may appear after macros are expanded, so this article will summarize some methods to view the code after macros are expanded.

 

1. in Visual Studio 2005, select project Properties> Configuration Properties> C/C ++> Preprocessor, and select generate preprocessing file in "generate preprocessing File, "No line number (/EP/P)" or "with line number (/P)", a file with the same name as the source file is generated in the folder where the project is located. "I" file, which is the code file after the macro is expanded.

Specific settings are as follows:

Original code macro. cpp

# Include <iostream>

# Include <cstdio>

# Define to_string_macro (x) # x

# Define a_testing_macro (n) printf ("A testing macro with a int argument n = % d! /N ", n)

# Define show_macro_content (m) to_string_macro (m)

 

Using namespace STD;

 

 

Int main (INT argc, char * argv [])

{

Int A = 10;

Int * B = &;

Cout <to_string_macro (a) <Endl;

Cout <to_string_macro (10 + 20) <Endl;

Cout <a_testing_macro (10 + 20) <Endl;

Cout <to_string_macro (a_testing_macro (A) <Endl;

Cout <show_macro_content (a_testing_macro (A) <Endl;

Cout <show_macro_content (a_testing_macro (* B) <Endl;

 

* B = 100;

A_testing_macro (* B );

Return 0;

}

 

 

 

Code Macro. I after macro expansion

 

Using namespace STD;

 

 

Int main (INT argc, char * argv [])

{

Int A = 10;

Int * B = &;

Cout <"A" <Endl;

Cout <"10 + 20" <Endl;

Cout <printf ("A testing macro with a int argument n = % d! /N ", 10 + 20) <Endl;

Cout <"a_testing_macro (a)" <Endl;

Cout <"printf (/" A testing macro with a int argument n = % d! // N/", a)" <Endl;

Cout <"printf (/" A testing macro with a int argument n = % d! // N/", * B)" <Endl;

 

* B = 100;

Printf ("A testing macro with a int argument n = % d! /N ", * B );

Return 0;

}

 

 

 

2. in Linux, use the GCC, G ++ compiler for compilation, and use the-P option of the compiler. If the source file macro. cpp exists, the specific command is as follows:

$ G ++-e-p macro. cpp> Macro. prescan

 

 

3. If you want to view the macro expansion status at runtime, you can refer to the source file macro. cpp and define three Macros in this file,

# Define to_string_macro (x) # x

# Define a_testing_macro (n) printf ("A testing macro with a int argument n = % d! /N ", n)

# Define show_macro_content (m) to_string_macro (m)

 

To_string_macro converts macro parameters to strings. However, if the macro parameter is a macro

 

Here we will talk about the expansion order of macros. If a macro has parameters, such as X in to_string_macro (x), we call them form parameters, and the actual macro parameters are called real parameters, such as to_string_macro () a.

Macro expansion is a complicated process, but you can simply describe it in the following three steps,

First, replace the form parameter with the real parameter to the macro text;

Then, if the real parameter is also a macro, the real parameter is expanded;

Finally, continue to process the macro text after the macro is replaced. If the macro text also contains the macro, continue to expand; otherwise, the expansion is completed.

 

However, there is an exception, that is, after the first step, after the real parameter is substituted into the macro text, if the real parameter encounters the character "#" or "#" before the real parameter, even if the real parameter is a macro, this parameter is not expanded, but used for text processing.

 

Take the macro to_string_macro as an example to briefly analyze the order of the macro.

# Define to_string_macro (x) # x

To_string_macro (a). During expansion, replace real parameter A with the macro content and then "# A". The macro converts real parameter A to text.

If the real parameter of the macro is also a macro, such as to_string_macro (a_testing_macro (a), it is first expanded as "# a_testing_macro (a)", macro a_testing_macro () if it is the character "#", it is not expanded but converted to text. Therefore, the expanded macro "a_testing_macro (a)" cannot be output.

To output the expanded macro a_testing_macro (a), we can define a macro

# Define show_macro_content (m) to_string_macro (m)

To output a macro, use the macro as follows.

Show_macro_content (a_testing_macro ())

 

The following describes the sequence of the macro:

First, replace the real parameter a_testing_macro (a) into the macro text to_string_macro (m) to get to_string_macro (a_testing_macro (A). Note that "#" is not encountered here. so expand the real parameter, that is, macro a_testing_macro (a). After the macro is fully expanded, the result is as follows: to_string_macro (printf ("A testing macro with a int argument n = % d! /N ", ))

Next, expand the macro to_string_macro to get a string that is the expanded macro a_testing_macro.

 

If the entire process is not clearly described above, you can take a look at the content after processing in the following steps to help understand

Show_macro_content (a_testing_macro ())

To_string_macro (a_testing_macro ())

To_string_macro (printf ("A testing macro with a int argument n = % d! /N ", ))

# Printf ("A testing macro with a int argument n = % d! /N ",)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.