Understanding of inline functions

Source: Internet
Author: User

What is an inline function?
Inline (caution, not online), translated into "inline" or "inline". Meaning: When the compiler discovers that a piece of code calls an inline function, it does not call the function, but instead inserts the code of the function into the current position. The benefit of this is that the process of calling is omitted and the program runs faster. (the function calls the procedure, because has previously said the parameter into the stack and so on operation, therefore must occupy some time more often). This is not good: Because whenever the code calls into an inline function, you need to insert the code of the function directly at the call, so the volume of the program increases. Take the life phenomenon analogy, like the TV is broken, through the telephone to find the repairman, you will be too slow, so simply at home to keep a repairman. This is, of course, fast, but the repairman is going to have to take the floor when he lives in your house. An inline function is not required, it is just a modification to improve speed. To modify a function to be inline, use the following format:
declaration or definition of an inline function
In a simple sentence, add an inline modifier before the function declaration or definition.
inline int max (int a, int b)
{
Return (A>B)? A:B;
}

The intrinsic nature of inline functions is that it saves time but consumes space.

Ii. rules of the inline function

(1), a function can be self-called self, called a recursive call (described later), a function containing a recursive call cannot be set to inline;

(2), the use of complex process Control statements: Loop statements and switch statements, can not be set to inline;

(3), because inline increases the volume of the characteristics, it is recommended that the code within the inline function should be very short. Preferably no more than 5 lines.

(4), inline only as a "request", in particular cases, the compiler will disregard the inline keyword, and forcing the function to become a normal function. When this occurs, the compiler gives a warning message.

(5), before you call an inline function, this function must be declared before or defined as inline, if the previous declaration as a normal function, and after the calling code is defined as an inline function, the program can be compiled, but the function does not implement inline. For example, the following code snippet:
The function is not declared inline at first:
void Foo ();
Then there is the code that calls it:
Foo ();
Only after the call has the function defined as inline:
inline void foo ()
{
......
}
The code is the Foo () function does not finally implement inline;

(6), for debugging convenience, when the program is in the debugging phase, all inline functions are not implemented.

Third, the use of inline functions should pay attention to the following issues:

(1) inline functions defined in one file cannot be used in another file. They are usually shared in the header file.
(2) The inline function should be concise, only a few statements, if the statement is more, not suitable for the definition of inline functions.
(3) in the inline function body, there cannot be a loop statement, an if statement, or a switch statement, otherwise the compiler will treat the function as a non-inline function, even if there is an inline keyword.
(4) The inline function is declared before the function is called. The keyword inline must be placed with the function definition body in order for the function to be inline, and only the inline is placed before the function declaration.

Understanding of inline functions

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.