Hi all,
I try to use -Werror -Wall for my project, which includes
-Wunused-function.
Unfortunately, I receive a couple of warnings from library header files
which include
static inline ... func() { ... }
These functions are indeed often unused, but as of my understanding this
should be perfectly fine with static inline functions (they just replace
macro definitions in a better way).
Am I wrong here? What's the best path to fix this?
Ish. They shuold probably just be "inline" without the static.
static functions are distinct functions per translation unit - so having
static functions in headers has a number of problems:
* The function definitions won't be deduplicated by the linker across
object files -> code bloat (larger binaries) & address inequality
* This can lead to technical (& undiagnosed) Undefined Behavior due to ODR
violations
Consider the following header:
static inline void f() { }
struct foo {
void g() { f();
};
Include that header in two .cpp files, compile and link the two objects
together and the program now violates the ODR
The violation is in 'g' which is a (non-static, in the sense we're talking
about here - though it could be class-static and still reproduce the
problem) inline function. Therefore its definition must be the same in
every translation unit - by "the same", the C++ standard says the same
sequence of tokens (which it is) and that every name lookup finds the same
entities (which it doesn't - because 'f' in one translation unit is a
distinct 'f' from the other translation unit because of the 'static'
keyword)).
So the assumption is that, generally, file/namespace-scope-static (as
opposed to class scoped static, which is a different thing) functions do
not go in headers. Or, put another way - static functions are there to be
used in the translation unit that contains their definition. So that's why
the warning fires on your code & why removing 'static' is a good/reasonable
fix to the warning and what it is, tangentially, trying to warn you about.
Hope that helps,
- Dave