static inline vs inline static
What is the correct way to use inline here
Both static inline
and inline static
are allowed and they mean the same thing. static inline
is preferred style.
should I inline this function
To answer this question you will need to benchmark your program both ways and find out which is faster.
From the C standard (6.7 Declarations)
declaration:
declaration-specifiers init-declarator-listopt ;
static_assert-declaration
declaration-specifiers:
storage-class-specifier declaration-specifiersopt
type-specifier declaration-specifiersopt
type-qualifier declaration-specifiersopt
function-specifier declaration-specifiersopt
alignment-specifier declaration-specifiersopt
It means that you may specify declaration specifiers in any order.
So for example all shown below function declarations declare the same one function.
#include <stdio.h>
static inline int getAreaIndex( void );
inline static int getAreaIndex( void );
int static inline getAreaIndex( void );
static int inline getAreaIndex( void );
inline int static getAreaIndex( void )
{
return 0;
}
int main(void)
{
return 0;
}
As for the inline function specifier then according to the C Standard (6.7.4 Function specifiers)
6 A function declared with an inline function specifier is an inline function. Making a ∗function an inline function suggests that calls to the function be as fast as possible.138)The extent to which such suggestions are effective is implementation-defined.
and there is a footnote
139) For example, an implementation might never perform inline substitution, or might only perform inline substitutions to calls in the scope of an inline declaration
Pay attention to that you should specify as the function parameter void
. Otherwise the compiler will decide that the number and types of arguments are deduced from a function call.