Skip to main content
herbert
Associate III
March 17, 2023
Solved

Use of inlined functions

Hello,

is there a difference in declaring a C function with the __INLINE resp. inline keyword or using __attribute__((always_inline)) besides the latter one generating a warning?

regards

Herbert

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
March 17, 2023

inline is a standard C function specifier. It's semantics is described in C99 6.7.4, the gist of which is

Making a function an inline function suggests that calls to the function be as

fast as possible. The extent to which such suggestions are effective is

implementation-defined.

In other words, it's only a hint, and the compiler may decide to ignore it, or to implement it only if it's convenient (e.g. inline it only to functions which are in the same source file and only those which are after definition of this function).

__attribute__((always_inline)) is a gcc-specific extension, documented as:

For functions declared inline, this attribute inlines the function independent of any restrictions that otherwise apply to inlining. Failure to inline such a function is diagnosed as an error.

JW

2 replies

waclawek.jan
waclawek.janBest answer
Super User
March 17, 2023

inline is a standard C function specifier. It's semantics is described in C99 6.7.4, the gist of which is

Making a function an inline function suggests that calls to the function be as

fast as possible. The extent to which such suggestions are effective is

implementation-defined.

In other words, it's only a hint, and the compiler may decide to ignore it, or to implement it only if it's convenient (e.g. inline it only to functions which are in the same source file and only those which are after definition of this function).

__attribute__((always_inline)) is a gcc-specific extension, documented as:

For functions declared inline, this attribute inlines the function independent of any restrictions that otherwise apply to inlining. Failure to inline such a function is diagnosed as an error.

JW

S.Ma
Principal
March 17, 2023

I would refrain to use a compiler dependent way.

Making sure a function is inined is for rare use-case.

Besides, the alternative way of inlining functions with #define would be a NO GO (harder debug, breakpoint, etc...)