Topic: Static local variables in inline functions
Author: stephen.clamage@sun.com (Steve Clamage)
Date: Thu, 20 May 2004 18:45:24 +0000 (UTC) Raw View
Matthias Hofmann wrote:
> Hello,
>
> I have just read section 7.1.2 / 4 and found that it says
>
> "[...] A static local variable in an extern inline function always refers to
> the same object. [...]"
The standard doesn't really need to make that statement, because the
"inline" keyword does not change the semantics of a function.
For an ordinary (non-inline) extern function, there is only one copy
in the entire program, and only one copy of a local static variable in
that function. The same is true (one copy of the function, one copy of
local static variables) of extern inline functions. The standard has
this sentence for extra clarity, because the rule is a change from the
rules in the ARM.
>
> However, it makes no mention of non-extern inline functions. Why doesn't it
> just say
>
> "[...] A static local variable in an inline function always refers to the
> same object. [...]"?
Because that isn't true. Like non-inline static functions, you can
have a different static inline functions in different modules. They
are different functions, even if they have the same name and type.
That is, if you have
static int foo() { return 0; }
static inline int bar() { return 1; }
in each of several translation units, each unit gets its own copy. The
functions are different in each unit. If foo or bar has a local static
variable, that variable is unique to that copy of the function.
---
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: Thu, 20 May 2004 21:10:31 +0000 (UTC) Raw View
On Thu, 20 May 2004 02:50:48 +0000 (UTC), hofmann@anvil-soft.com
("Matthias Hofmann") wrote:
> I have just read section 7.1.2 / 4 and found that it says
> "[...] A static local variable in an extern inline function always refers to
> the same object. [...]"
> However, it makes no mention of non-extern inline functions. Why doesn't it
> just say
> "[...] A static local variable in an inline function always refers to the
> same object. [...]"?
If it is not extern, there is no problem to resolve across translation
units. If the same static inline function appears in two translation
units, there are really two functions each with its own local static.
Maybe an example will help.
John
#ifndef JUNK_HPP
#define JUNK_HPP
#include <cassert>
inline void f1 (int y) {
static int x = 0;
assert(x ++ == y);
}
namespace {
inline void f2 (int y) {
static int x = 0;
assert(x ++ == y);
}
} // namespace
#endif
// junkm.cpp
#include "junk.hpp"
void g ();
int main () {
f1(0);
f2(0);
g();
f1(2);
f2(1);
}
// junks.cpp
#include "junk.hpp"
void g () {
f1(1);
f2(0);
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kuyper@wizard.net (James Kuyper)
Date: Fri, 21 May 2004 04:58:29 +0000 (UTC) Raw View
hofmann@anvil-soft.com ("Matthias Hofmann") wrote in message news:<c8gk6e$95u$1@news1.nefonline.de>...
> Hello,
>
> I have just read section 7.1.2 / 4 and found that it says
>
> "[...] A static local variable in an extern inline function always refers to
> the same object. [...]"
>
> However, it makes no mention of non-extern inline functions. Why doesn't it
> just say
>
> "[...] A static local variable in an inline function always refers to the
> same object. [...]"?
> Is there no guarantee about static local variables always refering to the
> same object when the inline function is declared static as well?
Correct.
Such a guarantee would be inconvenient for many implementations.
'inline' is only a suggestion, not a requirement, and in some contexts
a static inline function is defined as if it were simply static. A
static function's name can be used in multiple translation units
(possibly with a different definition in each one), and the static
local variables of those functions in those different TUs are
different variables.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: hofmann@anvil-soft.com ("Matthias Hofmann")
Date: Thu, 20 May 2004 02:50:48 +0000 (UTC) Raw View
Hello,
I have just read section 7.1.2 / 4 and found that it says
"[...] A static local variable in an extern inline function always refers to
the same object. [...]"
However, it makes no mention of non-extern inline functions. Why doesn't it
just say
"[...] A static local variable in an inline function always refers to the
same object. [...]"?
Is there no guarantee about static local variables always refering to the
same object when the inline function is declared static as well?
Best regards,
Matthias Hofmann
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]