C + + inline functions

Source: Internet
Author: User

Inline functions are functions that are decorated with the inline keyword. functions defined within a class are defaulted into inline functions.

Essence

is when an inline function needs to be called, instead of being called, the whole segment of the function code is inserted into the place where the inline function needs to be used, thus eliminating the call process and increasing the speed of the operation.

Disadvantages

Because every time the code calls into an inline function, you need to insert a section of the function's code directly at the call, so the volume of the program increases and consumes more space.

Inline functions are best for small function use, such as accessing private data members. The main purpose of these "accessor" functions for one or two lines of code is to return state information about the object, and short functions are sensitive to the overhead of function calls. Longer functions spend less time on the call/return sequence, while the benefits from inline are reduced.

1 //When_to_use_inline_functions.cpp2 class Point3 {  4  Public:  5     //Define "accessor" functions as6     //reference types. 7unsigned&x (); 8unsigned&y (); 9 Private:  Ten unsigned _x;  One unsigned _y;  A };  -    -Inline unsigned&point::x () the {   -     return_x;  - }   -Inline unsigned&point::y () + {   -     return_y;  + }   A intMain () at {   -}

Assuming that the operation coordinates are relatively common in such clients, specifying the two accessor functions (and in the preceding example x y ) as "inline" typically saves the cost of the following operations:

    • function calls (including parameter passing and placing object addresses on the stack)

    • Keep the caller's stack frame

    • Set a new stack frame

    • return value Communication

    • Old stack frame restore

    • Return

inline functions and macros:

Although inline functions are similar to macro (because the function code is expanded at compile-time calls), inline functions are parsed by the compiler, and macros are expanded through the preprocessor. Therefore, there are many significant differences:

  • Inline functions Follow all types of security protocols enforced on normal functions.
  • Use the same syntax as any other function to specify inline functions, except that they include the inline keyword in the function declaration.
  • Evaluates an expression that is passed as a parameter to an inline function once. In some cases, an expression passed as a parameter of a macro can be evaluated more than once.

The compiler cannot inline a function if the following conditions occur:

    • Compiles a function or its callers using/ob0 (the default option for debugging builds).

    • Functions and callers use different types of exception handling (one in C + + exception handling, and the other with structured exception handling).

    • The function has a variable argument list.

    • The function uses an inline assembly unless it is compiled with/og,/ox,/o1, or/o2.

    • Functions are recursive functions and do not come with #pragma inline_recursion (on). The recursive function uses a pragma inline as the default depth of 16 calls. To reduce the inline depth, use the inline_depth pragma.

    • A function is a virtual function that makes a dummy call. A direct call to a virtual function can be inline.

    • The program takes a function address, which is called by a pointer to a function. A direct call to a function that takes its address can be inline.

    • The function is also marked with the naked __declspec modifier.

C + + 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.