Preprocessor macro (Preprocessor Macros)

Pretreatment C macros
macro definition belongs to a pre-command.
Macro: C language standard allows a program identifier used to represent a string. Identifier is the macro name.
Macro substitution: macro substitution is a macro definition. In the preprocessor, the program to replace all of the macro name with the corresponding string, a process called macro substitution.

Be careful when using a macro, as much as possible instead of the inline function, and enumeration constants.
Macro code and compiler means that you see is different, which may cause abnormal behavior, especially if the macro exists in global scope.
Fortunately, C ++, the macro unlike C, so necessary.
Macro inline critical code efficiency (performance-criticalcode) may be replaced with an inline function;
macros can be stored constant constvariable substitution;
Macro "an abbreviation for" long variable names can be referenced alternative;
the use of macro conditional compilation, this ...... it is best not so It does, will make the test even more painful ( #defineto prevent the header file that contains heavy course, is an exception).

Macros can do some things that other technologies can not be achieved in some code base (especially the underlying database) you can see some of macros (such as a string of (stringifying, use #), connection (concatenation, use ##), etc.) . However, before use, you can not help but think about using macros to achieve the same effect.

Usage patterns given below to avoid some of the problems using a macro, use a macro for the reference:

  1. Do not define macros in .h file;
  2. Before using the correct #define, properly after use #undef;
  3. Do not just use existing macro #undef, select the name that will not conflict;
  4. Do not use C ++ structure will lead to an unstable macro, at least documented their behavior.

Reproduced in: https: //www.jianshu.com/p/9b7b764fe12a

Guess you like

Origin blog.csdn.net/weixin_33904756/article/details/91077518