C + + inline functions

Source: Internet
Author: User
Tags class definition function definition

Inline functions are functions that are decorated with the inline keyword. functions defined within a class are defaulted into inline functions. The inline function, from the source code layer, has the structure of the function, but after compiling it, it does not have the properties of the function.

Inline extensions are used to eliminate the time overhead of function calls . It is often used for functions that are frequently executed, and is very beneficial for functions in small memory spaces.

When using inline functions, be aware that:

    • Recursive functions cannot be defined as inline functions
    • Inline functions are generally suitable for small functions that do not have a complex structure such as while and switch, and only have a sentence, otherwise the compilation system treats the function as a normal function.
    • The inline function can only be defined and used, otherwise the compiler will think of it as a normal function.
    • An inline function cannot make an exception to the interface declaration.

Inline functions should be defined in the header file, which differs from other functions. The compiler must be able to find the definition of the inline function in order to replace the calling function with the function code, which is not sufficient for a function declaration in the header file, when the code for the expansion function is inline with the call point.

Of course the inline function definition can also be placed in the source file, but at this time only the definition of the source file can be used, and must be a copy of each source file definition (that is, each source file definition must be identical), of course, even in the header file, but also a copy of each definition, It's just a compiler doing this for you. But rather than being placed in a source file, placing it in a header file ensures that the calling function is the same definition, and that the function definition can be found at the call point to complete the inline (replace).

A function is defined as an inline function only if it is only 10 rows or less.

Definition: When a function is declared as an inline function, the compiler expands it, rather than invoking it as a normal function call mechanism.
Advantage: When the function body is relatively small, inline the function can make the target code more efficient. For functions with access functions and other functions that are short, performance-critical, the use of inline is encouraged.
Cons: Abusing the inline will cause the program to slow down. Inline may cause the target code to increase or decrease, depending on the size of the inline function. Inline very short access functions usually reduce the size of the code, but a fairly large number of inline functions will dramatically increase the size of the code. Modern processors because of the better use of the instruction cache, small code tends to execute faster.
Conclusion: a more reasonable rule of thumb is not to inline more than 10 rows of functions. The destructor is treated with caution, and destructors tend to look longer than their surface because there are hidden members and base class destructors called!
Another practical rule of thumb: inline functions that contain loops or switch statements are often not worth the candle (unless, in most cases, these loops or switch statements are never executed).
It is important that some functions are not necessarily inline by the compiler even if they are declared inline; For example, virtual functions and recursive functions are not normally inline. In general, recursive functions should not be declared as inline functions. (The expansion of the recursive call stack is not as simple as looping, such as recursive layers may be unknown at compile time, and most compilers do not support inline recursive functions). The main reason for the virtual function inline is to put its function body within the class definition, for convenience, or as a document to describe its behavior, such as a short access function.

Details reproduced: 50601800

C + + inline functions

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.