inline vs __inline vs __inline__ vs __forceinline?
Solution 1
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
__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
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
Related videos on Youtube
Xavier Ho
Curiosity-driven designer, researcher and software engineer. My passion lies somewhere in the spectrum of chocolates, video games, and a better world.
Updated on July 05, 2022Comments
-
Xavier Ho almost 2 years
What are the differences between these four inline (key)words?
inline
,__inline
,__inline__
,__forceinline
.-
Martin York about 14 yearsYou should also ask the community what the recommendation for usage of inline are.
-
-
Joris Timmermans about 14 years__forceinline is a more forceful hint than inline, but still just a hint (msdn.microsoft.com/en-us/library/z8y1yy88%28VS.80%29.aspx).
-
Dan about 14 yearsMaybe consider changing the "Microsoft-specific" language to "vendor-specific" or something like that. Many of the embedded cross-development toolsets I use also support __inline and __forceinline. The world is bigger than MSOFT ;-)
-
osirisgothra over 10 yearsAll __forceinline does is skip the analysis of the overhead and benefit factors of inlining and 'force' it to happen anyways. Unless you really need to to be inlined you are better off to just stick with inline when using C++ or __inline when using C. As far as i know, __forceinline originates from MSVC but it gets used and defined other places, notably by GCC (as stated above) in the attribute expression. But of course, you have to use the inline headers or flags for it to be used. Some IDEs will disable this by default, others enable it by default. I'd steer clear if youre a cross developer!
-
thomasrutter almost 3 yearsgcc suggests
__inline__
as an alternative toinline
for pre-C99