About __forceinline and __inline

2

What's the difference between using __forceinline or __inline ? And can you use __forceinline or __inline in large functions?

Example:

__forceinline void funcao1(void)
{
    cout << "Funcao 1" << endl;
}

__inline void funcao2(void)
{
   cout << "Funcao 2" << endl;
}
    
asked by anonymous 28.09.2017 / 20:26

1 answer

2

These instructions tell the compiler that you want the function to be linearized, that is, instead of calling it its code is placed where it had a call saving a few bytes and processing since it is no longer necessary to have the function header and footer and in some cases no longer need to copy argument data to the parameter.

inline is C ++ default and is a hint for the compiler to consider doing this optimization there. Not that he does not do it if he does not have these keywords, he can do it if he thinks he pays to optimize, even with nothing written. Instruction like this may increase the chance, but it depends on the compiler, nothing guarantees. Naga guarantees that the optimization is done, the compiler can recurs if he thinks it does not pay.

__inline is the same but is a Microsoft compiler extension. The exact way that the compiler handles depends on it.

__forceinline is just an indication that you really want the optimization done, so the chance is high, but it's not guaranteed either. If it is very clearly bad the compiler does not.

Being able to use large functions can, but is not recommended, and the compiler is likely to ignore. If you do not know if it will really be useful, do not use it, it's a very advanced feature.

Documentation .

Related:

28.09.2017 / 23:04