Definition and use of inline functions in C + +

Source: Internet
Author: User
Tags definition execution expression function definition

The purpose of introducing inline functions is to solve the problem of the efficiency of function calls in programs.

A function is a more advanced abstraction. The introduction of it makes the programmer only concerned about function and the use of functions, but do not need to care about the implementation of function functions, the introduction of functions can reduce the program's target code, to achieve program code and data sharing. However, a function call can also cause inefficiencies, because the calling function actually shifts the order of execution of the program to an address in memory that the function is stored in, and then returns to the place where the function was transferred before the function was executed. This transfer operation requires the protection of the site and memory of the execution of the address before the transfer, back to restore the scene first, and continue to carry out the original save address. Therefore, the function call has a certain amount of time and space overhead, so it will affect its efficiency. Especially for some function body code is not very large, but also frequently called function, to solve its efficiency problem is more important. The introduction of inline functions is actually to solve this problem.

At compile time, the compiler replaces the invocation expression of the inline function that appears in the program with the function body of the inline function. Obviously, this practice does not produce the problem of turning back, however, because the code in the function break is replaced in the program at compile time, it will increase the amount of the target program code, and then increase the space overhead, while the time distribution is not as big as the function call, it is the cost of increasing the target code in exchange for time saving.

Definition method of inline function

The way to define inline functions is simple, as long as you add the keyword inline to the head of the function definition. Inline functions are defined in the same way as normal functions. Such as:

inline int add_int (int x, int y, int z)

  {

  return x+y+z;

  }

In a program, when the function is called, the function is substituted at compile time, rather than being invoked at run time like a normal function.

Matters needing attention in using inline functions

The inline function has the characteristic of a general function, which differs from a general function in that it is handled by a function call. When a general function is invoked, the execution of the program is transferred to the called function, and then back to the function that called it, and the inline function replaces the invocation expression with the inline function body when invoked. When using inline functions, you should be aware of the following points:

1. A function defined within a class is an inline function, and a function defined outside the class is not an inline function (a short function can be defined within a class, and a long function can be defined outside of a class).

2. You can specify the inline keyword for functions that are defined outside the class, forcing an inline function.

3. Loop and switch statements are not allowed within the inline function.

4. The definition of an inline function must occur before the inline function is called for the first time. No message! You may be interested in the following information:

Related Article

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.