static inline vs inline static
Asked Answered
F

5

17

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)

Fyn answered 10/5, 2020 at 15:3 Comment(1)
For future reference, around here we strongly encourage asking only one question per post.Draughtboard
S
4

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.

Swat answered 10/5, 2020 at 15:13 Comment(11)
Thanks, may you help answer my second question too?Fyn
@smith_brown See my appended answer. It is the implementation that decides whether to make the function inline.Swat
@smith_brown I am saying about the parameter. If you will not specify parameters then the compiler will try to deduce the number and types of parameters when it encounters a function call that is error-probe.Swat
I didn't understand that, what parameter are you referring to? could you add simple example to explain it?Fyn
@smith_brown Did you specify parameters? No. There are an empty parentheses. So the compiler can say nothing about how many parameters the function actually have. It is only in C++ empty parentheses mean that the function has no parameters.Swat
Actually I have parameters (removed them to keep the code clean). in general I shouldn't care about this parameters things right?Fyn
@smith_brown You are wrong. In such a case the program is potentially error-prone.Swat
@smith_brown Why should you remove inline?Swat
@smith_brown I do not understand what you are saying. How are parameters connected with the function specifier inline?!Swat
you said "In such a case the program is potentially error-prone" what do u refer to in in such a case?Fyn
@smith_brown We are talking about parameters are not we? Please reread the comments.Swat
D
12

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.

Draughtboard answered 10/5, 2020 at 15:13 Comment(6)
I heard that inline will not cause a damage (or faster or nothing) but since I'm not using this function only inside loop I don't know if it's worth itFyn
Inlining definitely can make your program slower. The most common reason is if it makes your program big enough that it no longer fits in the instruction cache.Draughtboard
"static inline is preferred style." -- according to what source?Trigger
@JasonS According to the C standard itself, in fact. N1570 §6.11.5: "The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature."Draughtboard
mention that in your answer, then.Trigger
"The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature." That's a really weak statement, and it's still present in N2731 open-std.org/JTC1/SC22/WG14/www/docs/n2731.pdf I get why something like static int foo() would be preferred over int static foo() but why would inline static vs static inline matter?Trigger
S
4

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.

Swat answered 10/5, 2020 at 15:13 Comment(11)
Thanks, may you help answer my second question too?Fyn
@smith_brown See my appended answer. It is the implementation that decides whether to make the function inline.Swat
@smith_brown I am saying about the parameter. If you will not specify parameters then the compiler will try to deduce the number and types of parameters when it encounters a function call that is error-probe.Swat
I didn't understand that, what parameter are you referring to? could you add simple example to explain it?Fyn
@smith_brown Did you specify parameters? No. There are an empty parentheses. So the compiler can say nothing about how many parameters the function actually have. It is only in C++ empty parentheses mean that the function has no parameters.Swat
Actually I have parameters (removed them to keep the code clean). in general I shouldn't care about this parameters things right?Fyn
@smith_brown You are wrong. In such a case the program is potentially error-prone.Swat
@smith_brown Why should you remove inline?Swat
@smith_brown I do not understand what you are saying. How are parameters connected with the function specifier inline?!Swat
you said "In such a case the program is potentially error-prone" what do u refer to in in such a case?Fyn
@smith_brown We are talking about parameters are not we? Please reread the comments.Swat
C
2

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 specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

static being a "storage class specifier".

Corin answered 10/5, 2020 at 15:36 Comment(1)
it doesn't say "obsolete", it says "obsolescent" meaning "that they may be considered for withdrawal in future revisions of this International Standard". And there's no justification for this; to be honest I don't see why this is here, aside to try to discourage int static foo() instead of static int foo(). I wish they would say that the type-specifier and type-qualifier should be closer to the object name, or at least that storage-class-specifier and function-specifier were of equivalent importance.Trigger
E
1

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.

Elisabeth answered 10/5, 2020 at 15:12 Comment(1)
Thanks may you answer my second question too?Fyn
R
1

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

Ripplet answered 10/5, 2020 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.