[cfe-users] static inline functions in headers and -Wunused-function

David Blaikie via cfe-users cfe-users at lists.llvm.org
Wed Jan 27 08:11:26 PST 2016

On Wed, Jan 27, 2016 at 3:01 AM, Rainer Gerhards via cfe-users <
cfe-users at lists.llvm.org> wrote:

> 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

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'

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

> version: Ubuntu clang version 3.6.2-1 (tags/RELEASE_362/final) (based on
> LLVM 3.6.2)
> Any help is deeply appreciated.
> Rainer
> _______________________________________________
> cfe-users mailing list
> cfe-users at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20160127/eda4035d/attachment.html>

More information about the cfe-users mailing list