Topic: inline function with static data


Author: Michael Cook <mcook@cognex.com>
Date: 22 Feb 1995 22:49:26 GMT
Raw View
If I define an inline function that has a static local variable, I should get
exactly one instance of that variable, right?

The ARM doesn't seem to say so explicitly.  But it does say that inline
functions have exactly one definition.

Conceptually, it seems there should be only one.  But it also seems that
compilers might have a hard time getting that right.  Gcc gets it "right" if I
use the klunky `#pragma implementation' mechanism, but gets it "wrong"
otherwise.
--
 Michael Cook <mcook@cognex.com>
 Telephone: +1 508 650 3251, Fax: +1 508 650 3336
 Cognex Corporation, One Vision Drive, Natick, Massachusetts 01760-2059




Author: jason@cygnus.com (Jason Merrill)
Date: Thu, 23 Feb 1995 02:14:11 GMT
Raw View
>>>>> Michael Cook <mcook@cognex.com> writes:

> If I define an inline function that has a static local variable, I should
> get exactly one instance of that variable, right?

That depends on whether the inline function has external linkage or not.
If it does, then you should get exactly one instance of the variable.  If
it does not, then you should get one instance of the variable for each
translation unit that includes the function.

All member functions have external linkage.

> Conceptually, it seems there should be only one.  But it also seems that
> compilers might have a hard time getting that right.  Gcc gets it "right"
> if I use the klunky `#pragma implementation' mechanism, but gets it
> "wrong" otherwise.

Yep.

Jason