# C ++ foundation # inline __forceinline __attribute __ ((always_inline) inline function

inline 

is the C ++ keyword inline, the function declaration or definition, before the function returns with the keyword inline type, i.e., the function can be specified for inline functions. That could solve some of the problems frequently called functions consume a lot of stack space (stack memory). Keyword inline function definition must be placed together to make the function inline function , just in front of the inline function declaration has no effect. inline is a "for achieving" keyword, rather than a keyword "used to declare" the.

Error global function example: function_inline

inline void function_inline();
void function_inline()
{
    std::cout << __FILE__ << ":" << __LINE__ << "/" << __FUNCTION__ << std::endl;
}

Global function properly example: function_inline

inline void function_inline()
{
    std::cout << __FILE__ << ":" << __LINE__ << "/" << __FUNCTION__ << std::endl;
}

Examples of functions Error members: class_inline :: function_inline

//class_inline.h
class class_inline 
{
    public: 
    inline void function_inline();
};

//class_inline.cpp
void class_inline::function_inline()
{
    std::cout << __FILE__ << ":" << __LINE__ << "/" << __FUNCTION__ << std::endl;
}

Examples of functions correctly member: class_inline :: function_inline

recommend!

//class_inline.h
class class_inline 
{
public: 
    inline void function_inline() 
    {
        std::cout << __FILE__ << ":" << __LINE__ << "/" << __FUNCTION__ << std::endl;
    }
};

Not recommended! 

//class_inline.h
class class_inline 
{
public: 
    inline void function_inline();
};

//class_inline.cpp
inline void class_inline::function_inline()
{
    std::cout << __FILE__ << ":" << __LINE__ << "/" << __FUNCTION__ << std::endl;
}

Not recommended!  

//class_inline.h
class class_inline {
public: 
    void function_inline();
};

//class_inline.cpp
inline void class_inline::function_inline()
{
    std::cout << __FILE__ << ":" << __LINE__ << "/" << __FUNCTION__ << std::endl;
}

inline function is just a suggestion to the compiler, so in the end can really inline, see the compiler mean, if it believed the function is not complicated, can be deployed at the point of call, you will truly inline, does not mean that a statement within United will be inline, within the joint declaration just a suggestion only.

Also, this, for GCC/C99.

The extent to which suggestions made by using the inline function specifier are effective (C99 6.7.4).

  • GCC will not inline any functions if the -fno-inline option is used or if -O0 is used. Otherwise, GCC may still be unable to inline a function for many reasons; the -Winline option may be used to determine if a function has not been inlined and why not.

 

Luckily most of the compiler provides a diagnostic level: if you ask they will not function inline technology, will give you a warning message.

 

inline limit

Han only for the number of inline function body using a simple code. Not contain complex structures, for example, control statements while, switch, and can not inline function itself can not be directly recursive function (i.e., also call their own internal functions). And all (except the most mundane, almost nothing to do) virtual function will be to prevent the inline. Because virtual means "wait until run time and then determine which function to call," and it means inline "at compile time, will call the action to be replaced by the calling function of the body." If the compiler to make a decision, do not yet know what the call a function, it is difficult to instruct them to make an inline function.

 

Caution inline

Inline function is to improve the efficiency, code-expanded (copied) at the expense of only eliminating the function call overhead, thereby improving the efficiency of the function. If the execution time of the function body of code, compared to the function call overhead is large, the efficiency gains will be very limited. On the other hand, every call within inline function must copy the code, the total amount of code the program will increase, consume more memory space. Therefore, it is necessary to use discretion.

The following situations should not be used inline: 

  • (1) If the function code is relatively long in vivo, using inline memory consumption will lead to higher costs. 
  • (2) If the function body appears cycle, the time to perform a function within the code of a function call overhead than big.
  • (3) The constructor and destructor of misleading more effective to use inline. Beware constructor and destructor may hide some behaviors, such as the "secretly" implementation of the constructor and destructor member of a class or group of objects. Therefore, the definition body should not be constructors and destructors in the class declaration.

A good compiler will be based on the definition of body function, automatically canceled inline worthless (which further illustrates the inline should not appear in the function declaration). That "inline function is just a suggestion to the compiler, so in the end can really inline, see the compiler mean" when there is a clear need to compile macros using inline, use __forceinline __attribute __ ((always_inline)

 

__forceinline

The compiler attempts to inline functions, regardless of the characteristic function, and in some cases, the compiler might choose to ignore __forceinline, rather than inline functions. E.g:

  •     Recursive function never inlined into itself.
  •     Use alloca () function of the control is not.

When there are compile macros ( COMPLIE_ZMODULE_NAME time) replaced, it is best to ensure mandatory inline debug version (not compiled inline function, even if the addition of inline keyword) and the release version (may be optimized for inline functions), resulting in COMPLIE_ZMODULE_NAME content differences cause problems.

__forceinline void function_inline()
{
    std::cout << __FILE__ << ":" << __LINE__ << "/" << __FUNCTION__ << "COMPLIE_ZMODULE_NAME:" << COMPLIE_ZMODULE_NAME << std::endl;
}

__attribute__((always_inline)

The compiler attempts to inline functions, regardless of the characteristic function, in some cases, the compiler may choose to ignore __attribute __ ((always_inline) function attributes, e.g. without inline functions:

  •     Recursive function never inlined into itself.
  •     Use alloca () function of the control is not.

When there are compile macros ( COMPLIE_ZMODULE_NAME time) replaced, it is best to ensure mandatory inline debug version (not compiled inline function, even if the addition of inline keyword) and the release version (may be optimized for inline functions), resulting in COMPLIE_ZMODULE_NAME content differences cause problems.

inline void __attribute__((always_inline)) function_inline()
{
    std::cout << __FILE__ << ":" << __LINE__ << "/" << __FUNCTION__ << "COMPLIE_ZMODULE_NAME:" << COMPLIE_ZMODULE_NAME << std::endl;
}

 

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/104965726