[C ++] macro definition and inline function

xiaoxiao2021-03-06  98

[C ] macro definition and inline function

Macro code characteristics

The macro code itself is not a function, but uses the icon function. Pre-regulators use the copy macro code instead of the function call, save the parameter stack, generate the CALL call of the assembly language, return to the parameters, execute Return, etc., thereby improving the speed. The maximum disadvantage of using macro code is easy to make mistakes, and the preprocessor is often unexpected when copying macro code. E.g

??? #define max (a, b) ?????? (a)> (b)? (a): (b)

Statement

Result = max (i, j) 2;

Explain the pre-processor

??? result = (i)> (j)? (i): (j) 2;

Since the operator ' ' ratio operator ':' is high, the above statement does not equivalent to expectations.

??? result = ((i)> (j)? (i): (j)) 2;

If the macro code is rewritten as

??? #define max (a, b) ?????? ((a)> (b)? (a): (b))

The errors caused by priority can be resolved. But even if you use the modified macro code, it is not very unhappy, such as statement?

Result = max (i , j);

Explain the pre-processor

??? result = (i )> (j)? (i ): (j);

For C , there is another disadvantage to use the macro code: a private data member that cannot be operated.

?

Inline function inline characteristics

For any end function, the compiler is placed in a function of the function (including the name, parameter type, and the return value type). If the compiler does not find an inner function error, the code of the function is also placed in a symbol table. When calling an inline function, the compiler first checks if the call is correct (for type security check, or automatic type conversion, of course, all functions). If it is correct, the code of the inline function will replace the function calls directly, so I save the overhead of the function call. This process is significantly different from pretreatment because the preprocessor cannot perform type security checks, or automatic type conversion. If the inline function is a member function, the address of the object (this) will be placed in a suitable place, which is also not possible.

The function inline mechanism of the C language has both the efficiency of macro code, but also the security of the information, and the data member of the class. So in the C program, you should use the inner function to replace all macro code, "assertion assert" is probably the only exception. Assert is a macro that works only on the Debug version, which is used to check the situation that "should not" happen. In order not to cause a difference between the Debug version and the Release version, Assert should not generate any side effects. If the assert is a function, since the function call causes the memory, the code changes, then there will be differences in the DEBUG version and the Release version. So Assert is not a function, but a macro.

[Note] Keyword Inline must be placed with the function definition to make the function be inline, only in front of the function declaration.

Defining member functions in class declarations will automatically become inline functions

?

转载请注明原文地址:https://www.9cbs.com/read-123488.html

New Post(0)