Talk about the C + + Session 2 inline (inline) function

Source: Internet
Author: User

definition: An inline function is an inline extension that saves overhead to function calls by inserting function code at the address of each function call, avoiding jumping to a subroutine. The inline keyword is similar to macro, where the compiler places a new copy of the inline function where it is called, and the inline function runs faster than the normal function call, and the overhead is saved, but there is a memory problem. If an inline function is called 10 times, there will be 10 copies of the function inserted into the code. As a result, inline functions are the best small features that are often called. The member function of a class, if defined in the class definition, is necessary by default (no need to use the inline keyword), otherwise the keyword is required. The compiler may ignore the programmer's attempt to inline the function.
"Examples"actually, it's still very useful.
//function examples
inline int max (int a, int b) {return (a > B) a:b;}  C + + #include <iostream.h>int increment (int i), void Main () {int i=0; while (i<3) {i=increment (i); cout<< "I is" <<i<<endl;}} The inline function definition is placed after the main () function inline int increment (int i) {i++; return i;}


"Non-standard usage of sample 2,vc++"no one wants to
refer to MSDN (too lazy to translate, see yourself Google Translate)
The function or its caller are compiled with/ob0 (the default option for debug builds).
the function and the caller use different types of exception handling (c + + exception handling in one, structured excep tion handling in the other).
The function has a variable argument list.
The function uses inline assembly, unless compiled with/og,/ox ,/o1, Or/o2.
The function is recursive and not accompanied by #pragma inline_recursion (on). With the pragma, recursive functions is inlined to a default depth of calls. To reduce the inlining depth, use inline_depth pragma.
The function is virtual and is called virtually. Direct calls to virtual functions can is inlined.
The program takes the address of the the function and the made via the pointer to the function. Direct calls to functions that has had their address taken can be inlined.
The function is a also marked with the naked __declspec modifier.
__forceinline is useful if:


inline or __inline is not respected by the compiler (ignored by compiler Cost/benefit Analyzer)
code Portability is not required
inlining results in a necessary performance boost
Example of portable code:


#ifdef _msc_ver#define Inline __forceinline/* Use __forceinline (VC + + specific) */#else # define INLINE inline/* Use stand ARD inline */#endifINLINE void HelloWorld () {/* Inline function body */}


Talk about the C + + Session 2 inline (inline) function

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.