Difference between an inline function and static inline function
The non-static inline
function declaration refers to the same function in every translation unit (source file) that uses it.
The One Definition Rule requires that the body of the function definition is identical in every TU that contains it, with a longish definition of "identical". This is usually satisfied provided that the source files all use the same header, and provided that the function doesn't use any global names with internal linkage (including static
functions) or any macros that are defined differently in different TUs.
I don't remember encountering that particular linker error before, but it's at least possible that one of these restrictions is responsible. It's your responsibility to satisfy the requirements: undefined behavior with no diagnostic required if you don't.
The static inline
function declaration refers to a different function in each translation unit, that just so happens to have the same name. It can use static
global names or macros that are different in different TUs, in which case the function might behave differently in the different TUs, even though its definition in the header file "looks the same".
Because of this difference, if the function contains any static
local variables then it behaves differently according to whether it is static
or not. If it is static
then each TU has its own version of the function and hence its own copy of the static
local variables. If it's inline
only, then there is only one copy of the static
local variables used by all TUs.
Comments
-
Vijay over 4 years
Can anybody tell me what the difference is between an inline function and static inline function?
In which cases should I prefer static inline over inline?
I am asking this question because I have an inline function for which I am facing compilation issues during linking (
relocation error:... symbol has been discarded with discarded section ...
). I made it a normal function and it worked. Now some of my seniors told me try with static inline. Below is my function:inline void wizSendNotifier (const char* nn_name, bpDU* arg=0, int aspect = -1) { wizuiNotifier* notifier = ::wizNtrKit.getNotifier (nn_name); notifier->notify (arg, aspect); }
and this not inside a class. This is inside a header file!
I guess the call to a static function should be done only in the particular TU where it is defined.
Since my function is in a header file and if i make it static, will it be the case that where ever I include that header file the static function can used used in that translation unit?
-
anatolyg over 11 yearsIs this function inside a class? Please also post its declaration.
-
Walter over 11 yearsis
::wizNtrKit
a static object? what's its linkage? -
Steve Jessop over 11 yearsAlso, where does the function appear? Header file, source file? How many source files include it?
-
Vijay over 11 yearsthe function appears in a header file steve.there are around 50 source file whete the header is included
-
-
Matthieu M. over 11 yearsNote: in the case of
static
functions, theinline
bit loses its semantic meaning (it's okay if that function definition appears in multiple TUs); the only thing which remains attached toinline
is a mere hint to the compiler, that most nearly ignore. -
Steve Jessop over 11 yearsYes, what I say about the
inline
function would also be true of function declarations that are neitherstatic
norinline
, except for the One Definition Rule requirement. Instead of requiring identical definitions in each TU, it would require that only one TU contains a definition at all. Given that the questioner's code works wheninline
is removed I suspect there's something funny going on here -- you'd expect a linker error if it's defined in multiple TUs. I don't think a diagnostic is required but it's not a difficult one for the linker.