Differences between inline functions and macros

Source: Internet
Author: User

The Inline Function Format is as follows:

Inline int FAC (int I) {return I * I ;}

Inline functions, like common functions, require reference type checks, but it is more efficient to execute inline functions than to execute common functions, because calls to inline functions do not need to be interrupted, inline functions are embedded into the target during compilation.CodeMedium.

Applicable scenarios:

1) the code is short and does not include loops.

2) need to be called repeatedly

The macro format is as follows (Note: There is no semicolon ):

# Define min (A, B) (a)> (B )? (B): (a) # define FAC (I) (I) * (I ))

Macros are only simple replacement of characters and are completed during preprocessing. macros are important in C, but are rarely used in C ++.

The differences between the two are summarized below:

1. The macro character replacement is completed during preprocessing (executed by the Preprocessor), and the inline function is embedded into the target code (executed by the compiler) during compilation)

2. inline functions are safer, because inline functions require parameter type check, while macros only replace simple characters.

Refer:

1 .《ProgramMember interview book (second edition): 6.4 inline functions and macro definitions of e-Industry Press

2. Baidu Encyclopedia: preprocessing

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.