'static inline vs inline static

I have noticed that both work, what is the correct way to use inline here?

static inline int getAreaIndex()

OR

inline static int getAreaIndex()

Plus, getAreaIndex contains a large loop. sometimes I call it only one and sometimes I call it through a loop, should I inline it? (it's 10 line tall)



Solution 1:[1]

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, because "storage class specifiers" like static are always supposed to come first in a declaration (see C11 ยง6.11.5).

should I inline this function

To answer this question you will need to benchmark your program both ways and find out which is faster.

Solution 2:[2]

Function specifiers, such as inline, and storage class specifiers, such as static, may appear in any order as part of a function declaration.

So both examples above mean exactly the same thing.

As for whether you should inline, the details of exactly what inline does implementation defined. So you should look up the documentation of your compiler to see.

Solution 3:[3]

They are functionally equivalent at the moment, but static inline is the correct way to write C. This is because of C17 having made other styles obsolete and bad practice:

6.11.5 Storage-class specifier

The placement of a storage-class speci?er other than at the beginning of the declaration speci?ers in a declaration is an obsolescent feature.

static being a "storage class specifier".

Solution 4:[4]

should I inline it?

Inline is just a hint for the compiler and the compiler is free to disregard it if it would be too detrimental for performance.

Also, for locally defined functions (in the same .c file/translation unit), the compiler can freely decide to inline a function, even if it was not marked as such.

In most cases, for static function not defined in headers, I believe it is preferred not to specify inline, and let the compiler inline the function as it sees fit, according to the optimization options you are providing it (-O<n>, -Ofast, -Osize).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Community
Solution 4 Julien Thierry