 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthias Hofmann Guest
|
Posted: Thu May 20, 2004 2:50 am Post subject: Static local variables in inline functions |
|
|
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 ]
|
|
| Back to top |
|
 |
Steve Clamage Guest
|
Posted: Thu May 20, 2004 6:45 pm Post subject: Re: Static local variables in inline functions |
|
|
Matthias Hofmann wrote:
| Quote: | 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.
| Quote: |
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, [email]stephen.clamage (AT) sun (DOT) com[/email]
---
[ 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 ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Thu May 20, 2004 9:10 pm Post subject: Re: Static local variables in inline functions |
|
|
On Thu, 20 May 2004 02:50:48 +0000 (UTC), [email]hofmann (AT) anvil-soft (DOT) com[/email]
("Matthias Hofmann") wrote:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
James Kuyper Guest
|
Posted: Fri May 21, 2004 4:58 am Post subject: Re: Static local variables in inline functions |
|
|
[email]hofmann (AT) anvil-soft (DOT) com[/email] ("Matthias Hofmann") wrote in message news:<c8gk6e$95u$1 (AT) news1 (DOT) nefonline.de>...
| Quote: | 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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|