inline functions in C + +

Source: Internet
Author: User

In C, you can use # define to improve execution efficiency, but error-prone, and in the case of C + + support functions inline, the purpose is also the same.

(1) The working principle of inline:

For any inline function, the compiler puts a function declaration (including first name, parameter type, return value type) in the symbol table. If the compiler does not find an inline function error, the function's code is also placed in the symbol table. When an inline function is called, the compiler first checks that the call is correct, such as type safety checks, type conversions, and so on. If correct, the code of the connotation function will replace the function call directly, thus eliminating the overhead of the function call.

This is different from # define, because # define does not perform type safety checks, or automatic type conversions. If an inline function is a member function, the address of the object is placed in the appropriate place, and this is not a preprocessing.

(2) inline programming style:

The keyword inline must be put together with the function definition to make the function inline, and it is not valid to put inline in a function declaration, for example:

inline void foo (void); is not valid only with declarations

void foo (void)

{...}

It is effective to put together the definition body, for example:

void foo (void);

inline void foo ()

{....}

As you can see, inline is a keyword for "implementation," but it's personal sense to also write inline in the statement, although this may lead to confusing statements and definitions, but it's easy for your little friends to be at a glance.

(3) Use of inline:

To some extent, the use of inline does improve the efficiency of function execution, but it is still advisable to use inline with caution. The reason for this is that inline is at the expense of code replication, which simply eliminates the call cost of the function, but the time to execute the function body code becomes longer. Therefore, the code in the inline function body should not be too long.

Of course, some compilers automatically cancel the unworthy inline based on the definition of the function, but I haven't noticed yet. When we find it, we'll add ^_^.

inline functions in C + +

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.