Macro and inline functions

Source: Internet
Author: User

For a complete description of macros and inline functions, see <C ++ programming philosophy> Chapter 1 inline functions

Http://wenku.baidu.com/view/dbf9051c59eef8c75fbfb39f.html

 

This article from http://hi.baidu.com/liugang585570/blog/item/83fa38f9c9d8fd04d8f9fd04.html

 

 

Before introducing inline functions, it is necessary to introduce preprocessing macros. The functions of inline functions are similar to those of preprocessing macros. We believe that we have used preprocessing macros. We often define some macros, such

# Define table_comp (x)> 0? (X): 0)

 

A macro is defined.

Why use Macros? Because the function must call the program

The execution sequence is transferred to an address stored in the memory of the function. After the program content of the function is executed, it is returned to the place before the function is executed. This transfer operation requires that the on-site address should be stored and remembered before the transfer is executed. After the transfer, the on-site address should be restored and the operation should be continued based on the original storage address. Therefore, function calling requires a certain amount of time and space overhead, which affects its efficiency. Macros only expand the code in the pre-processing area without additional space and time overhead. Therefore, calling a macro is more efficient than calling a function.

However, there are many unsatisfactory aspects of Macro.

1. Macros cannot access private members of objects.

2. macro definition is easy to generate two meanings.

For example:

# Define table_multi (X)
(X * X)

We use a number to call it, table_multi (10). It seems that there is no error. The result returns 100, which is correct, but if we use table_multi (10 + 10) the expected result is 400, while the macro call result is (10 + 10*10 + 10) and the result is 120. This is obviously not the expected result. To avoid these errors, add brackets to macro parameters.

# Define table_multi (X)
(X) * (x ))

This ensures that no error is made. However, even if this definition is used, this macro may still fail. For example, you can call table_multi (A ++) to call it, they wanted to get the results (a + 1) * (a + 1). What is actually? Let's look at the macro expansion result:
(A ++) * (a ++). If the value of A is 4, the result is 5*6 = 30. The expected result is 5*5 = 25, which causes another problem. In fact, these problems also exist in some C library functions. For example:
Toupper (* pchar ++) performs the ++ operation twice on pchar, because toupper is actually a macro.

We can see that macro has some unavoidable problems. How can we solve them?

The following describes how to solve these problems using the inline functions I want to introduce. We can use inline functions to replace macro definitions. In fact, we can replace the pre-processing macro With inline functions.

The difference between an inline function and a macro is that a macro is replaced by a Preprocessor, while an inline function is implemented through compiler control. In addition, inline functions are real functions, but they are expanded like macros when needed. Therefore, the parameter pressure stack of the function is removed, reducing the call overhead. You can call inline functions like calling functions without worrying about macro processing issues.

We can use inline to define inline functions. However, any function defined in the description section of the class will be automatically considered as an inline function.

Next we will introduce the usage of inline functions.

Inline functions are valid only when they are declared together with the function body. An inline statement like this
Tablefunction (int I) is ineffective. The Compiler just uses the function as a normal function declaration. We must define the function body.

Inline tablefunction (int I) {return
I * I };

This defines an inline function. We can call it as a common function. However, the execution speed is indeed faster than that of common functions.

We can also define external functions defined in the class as inline functions, such:

Class tableclass {
PRIVATE:
Int I, J;
Public:
Int add (){
Return I + J ;};
Inline int Dec () {return I-j ;}
Int
Getnum ();
}
Inline int tableclass: getnum (){
Return
I;
}

All the three functions stated above are inline functions. In C ++, the function of the function body is defined inside the class and is considered as an inline function by default. Whether or not you have the inline keyword.

Inline functions are the most widely used in C ++ classes and should be used to define access functions. Generally, data members are defined as private or protected in the classes we define. In this way, the outside world cannot directly read and write the data of our class members. To read and write private or protected members, you must use the member interface function. If we define these read/write member functions as inline functions, the efficiency will be better.

Class sample {
PRIVATE:
Int ntest;
Public:
Int readtest (){
Return ntest ;}
Void settest (int I) {ntest = I ;}
}

Of course, inline functions also have some limitations. That is, the Execution Code in the function cannot be too much. If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in the normal way. In this way, the efficiency of inline functions is the same as that of normal functions.

//************************************** **************************************** **************************************** *

(1) What is an inline function?
Inline functions are member functions defined in the class body, that is, the function body of the function is placed in the class body.

(2) Why should I introduce inline functions?
Of course, the main purpose of introducing inline functions is to solve the problem of function calling efficiency in programs. In addition, we talked about macros. There is an example:
# Define
ABS (x)> 0?
(X):-(x ))
When ++ I appears, macros will distort our meaning. In other words, macro definitions can easily produce two meanings.
  
We can see that macro has some unavoidable problems. How can we solve them? We have tried our best to replace the above.

Next we will use inline functions to solve these problems.

(3) Why can inline replace Macros?
1,
Inline functions of classes defined by inline. The code of the functions is put into the symbol table and replaced directly during use (expanded like a macro), without the call overhead and high efficiency.
2,
Obviously, the inline function of a class is also a real function. When the compiler calls an inline function, it first checks its parameter type to ensure that the call is correct. Then perform a series of related checks, just like treating any real function. This eliminates its hidden dangers and limitations.
3,
Inline
It can be used as a member function of a class. Of course, it can be used to protect members and private members of the class.

(4) What is the difference between inline functions and Macros?
The difference between an inline function and a macro is that a macro is replaced by a Preprocessor, while an inline function is implemented through compiler control. In addition, inline functions are real functions, but they are expanded like macros when needed. Therefore, the parameter pressure stack of the function is removed, reducing the call overhead. You can call inline functions like calling functions without worrying about macro processing issues. Compared with macro definitions with parameters, inline functions have the same code efficiency, but inline functions are better than macro functions because inline functions follow the type and scope rules, it is more similar to General functions. In some compilers, once the inline extension is closed, it will be called like a general function, which is more convenient.

(5) When should I use inline functions?
Inline functions are the most widely used in C ++ classes and should be used to define access functions. Generally, data members are defined as private or protected in the classes we define. In this way, the outside world cannot directly read and write the data of our class members. To read and write private or protected members, you must use the member interface function. If we read and write these
If the member function is defined as an inline function, the efficiency will be better.
Class
A
{
PRIVATE:
Int ntest;
Public:
Int readtest () {return
Ntest ;}
Void settest (int I) {ntest = I;
}
}

(6) How to Use inline functions?
We can use inline to define inline functions.
Inline int A (int x ){
Return 2 * X;
}
However, any function defined in the description section of the class will be automatically considered as an inline function.

(7) What are the advantages and disadvantages of inline functions?
We can call this function as a common function. However, because an inline function is expanded like a macro when needed, the execution speed is faster than that of a General function. Of course, inline functions also have some limitations. That is, the Execution Code in the function cannot be too much. If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in the normal way. (In other words, if you use an inline function, you only apply to the compiler. the compiler can reject your application.) in this way, inline functions are executed at the same efficiency as normal functions.

(8) How do I disable inline functions?
If you use VC ++, you can use the/ob command line parameter. Of course, it can also be used in the program
# Pragma
Auto_inline achieves the same purpose.

(9) considerations:
1. Loop statements and switch statements are not allowed in inline functions.
2. the inline function must be defined before the first call of the inline function.

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.