static inline __attribute__((always_inline))
If I use this before every function definition in a header will it guarantee inlining and will unused functions not be in the translation unit it’s included in?
CodePudding user response:
Generally speaking, there is no guarantee, that a function will be inlined. The inline keyword is just a hint for the compiler.
From the Clang reference (always_inline
):
Inlining heuristics are disabled and inlining is always attempted regardless of optimization level.
Does not guarantee that inline substitution actually occurs.
References:
- cppreference.com - inline function specifier
- Attributes in Clang - always_inline, __force_inline
- Clang Static Analyzer - Inlining
CodePudding user response:
static inline __attribute__((always_inline))
for a header only library?
No, just static inline
. static
so that the function can be in a header, inline
to silence compiler warnings. No __attribute__
, as it would make the library non-portable with a setback that long functions will be inlined increasing code size.
Let the user use inlining that he wants - -O3
will inline more, -Os
will inline less.
f I use this before every function definition in a header will it guarantee inlining
Yes.
will unused functions not be in the translation unit it’s included in?
They will be in the translation unit. They are defined in that translation unit.
Unused static
functions will not be included in the resulting object code coming from compiling that translation unit. This is unrelated to using __attribute__((always_inline))
.