static used in non-static inline function ?

R

regis

Greetings,

static int mode= 1;
static int half0 (int x) { return x / 2; }
static int half1 (int x) { return x >> 1; }
inline int half (int x) { return mode ? half1 (x) : half0 (x); }

Since version 4.3, it seems that gcc with -std=c99 warns:
mode is static but used in inline function half which is not static
half1 is static but used in inline function half which is not static
half0 is static but used in inline function half which is not static

(Same warnings if half0 and half1 are static inline)

As far as C99 is concerned, Is gcc right
for these uses of static variables and static functions
in inline non-static functions ?

That is, is it illegal ?

Thanks,
 
J

jameskuyper

christian.bau said:
C99 Standard draft 6.7.4.3: "An inline deï¬nition of a function with
external linkage shall not contain a deï¬nition of a modiï¬able object
with static storage duration, and shall not contain a reference to an
identiï¬er with internal linkage."

The reason for this requirement is that a compiler is free to create
both an actual function with external linkage that has only one
definition, and seperate inline code in every translation unit that
refers to the function. If it isn't meant to be used in mutliple
translation units, it shouldn't have external linkage.

The real function and the inlined equivalents are supposed to have
identical behavior, so it doesn't matter which one is used, and the
compiler is free to make a different choice at different locations in
your code. However, if the function refers to identifiers with
internal linkage, or defines objects of static storage duration, it
could make a difference which one was used. In your case, half1 and
half2 should be identical in every translation unit, so they're not
actually a problem. However, the standard is not intended to require
compilers to be smart enough to notice that fact; referring to them is
illegal.

The real problem is that there will be a separate copy of mode in each
translation unit, so the behavior of different copies of half() could
be different. If you want them to be different, you should declare
half() as static. If you want them to all be the same, remove 'static'
from half0, and half1, and replace static with 'extern' for mode, and
make sure that an actual definition for mode is provided in one an
only one translation unit.
 
L

lawrence.jones

regis said:
As far as C99 is concerned, Is gcc right
for these uses of static variables and static functions
in inline non-static functions ?

Yes -- 6.7.4p3 Constraints:

An inline definition of a function with external linkage ...
shall not contain a reference to an identifier with internal
linkage.
 
R

regis

The reason for this requirement is that a compiler is free to create
both an actual function with external linkage that has only one
definition, and seperate inline code in every translation unit that
refers to the function. If it isn't meant to be used in mutliple
translation units, it shouldn't have external linkage.

Thank you.
It seems that i'll have to remove one of the specifiers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top