Summary of C + + inline functions

Source: Internet
Author: User

C + + inline functions

inline functions, which look like functions, action-like functions, and no side effects of macros, can call them without the extra overhead of function calls. How good inline functions

In fact, getting more than you think, the extra overhead of avoiding function calls is just part of its benefits. The compiler can also use the optimization mechanism (which is designed to condense code that does not contain function calls), so when a function is inline, the compiler may be able to perform context-sensitive optimizations on it.

However, function calls increase the size of the target code, however, if the body of the inline function is small, the code that the compiler produces for the ' function body ' may be smaller than the target code produced by the ' function call '.

Summarize:

Advantages of inline functions:

1 No side effects of macros

2 No additional overhead for function calls

3 compiler optimizes code with optimization mechanism

Disadvantages:

It may be that the target code is enlarged, so when used, the function body must be small enough

Use Note:

1 Inline declaring a function as an inline function is just a request to the compiler, not a mandatory

2 The definition of a function is inline by default within the class definition

3 destructors and constructors are often bad candidates for inline (think about what the destructor is doing)

4 The inline function is inline also depends on how it is called

such as: inline void fuc () {}

typedef void (*PF) ();

PF PF = fuc;

FUC (); inline, Normal call

PF (); //Not necessarily inline, called by a function pointer

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summary of 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.