inline vs __inline vs __inline__ vs __forceinline?

C++InlineKeyword

C++ Problem Overview


What are the differences between these four inline (key)words?

inline, __inline, __inline__, __forceinline.

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

__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.

Solution 3 - C++

For the Visual Studio compiler it means:

  • inline - suggestion to the compiler to inline your code

  • __forceinline - overrides the builtin compiler optimization and generates inline code

For more details see: http://msdn.microsoft.com/en-us/library/z8y1yy88%28VS.71%29.aspx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXavier HoView Question on Stackoverflow
Solution 1 - C++kennytmView Answer on Stackoverflow
Solution 2 - C++CB BaileyView Answer on Stackoverflow
Solution 3 - C++codencandyView Answer on Stackoverflow