Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline vs __inline vs __inline__ vs __forceinline?

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

inline, __inline, __inline__, __forceinline.

like image 509
Xavier Ho Avatar asked May 04 '10 12:05

Xavier Ho


People also ask

What does __ Forceinline do?

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.

Is inline keyword useless?

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.

What is inline specifier in C++?

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.

Why inline function is used in C++?

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.


2 Answers

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.

like image 191
5 revs, 2 users 95% Avatar answered Oct 09 '22 11:10

5 revs, 2 users 95%


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

like image 43
CB Bailey Avatar answered Oct 09 '22 12:10

CB Bailey