What are the differences between these four inline (key)words?
inline
, __inline
, __inline__
, __forceinline
.
The __forceinline keyword forces the compiler to compile a C or C++ function inline. The semantics of __forceinline are exactly the same as those of the C++ inline keyword.
As compilers became better at optimising, this functionality has receded, and using inline as a suggestion to inline a function is indeed obsolete. The compiler will happily ignore it and inline something else entirely if it finds that's a better optimisation.
The inline specifier, when used in a decl-specifier-seq of a variable with static storage duration (static class member or namespace-scope variable), declares the variable to be an inline variable. A static member variable (but not a namespace-scope variable) declared constexpr is implicitly an inline variable.
Inline functions provide following advantages: 1) Function call overhead doesn't occur. 2) It also saves the overhead of push/pop variables on the stack when function is called. 3) It also saves overhead of a return call from a function.
inline
is the keyword, in C++ and C99.
__inline
is a vendor-specific keyword (e.g. MSVC) for inline function in C, since C89 doesn't have it.
__inline__
is similar to __inline
but is from another set of compilers.
__forceinline
is another vendor-specific (mainly MSVC) keyword, which will apply more force to inline the function than the __inline
hint (e.g. inline even if it result in worse code).
There's also __attribute__((always_inline))
in GCC and clang.
__inline
, __inline__
and __forceinline
are all implementation specific. Because of the double underscore they are all identifiers reserved for the implementation so shouldn't conflict with identifiers used in applications.
inline
is the only C++ keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With