Re: Static local variable in an inline static member function
On 26 Okt., 02:23, "lhy...@princeton.edu" <lhy...@gmail.com> wrote:
struct A {
static A& get() {
static A a;
return a;
}
};
OK, this is Meyers singleton.
[..]
If this appears in a header file that is then included in multiple
translation units, is it supposed to be guaranteed that there is only
one copy of the static A object?
Yes, this is guaranteed, see [dcl.fct.spec]/4:
"[..] An inline function with external linkage shall have the same
address in all translation units. A static local variable in an extern
inline function always refers to the same object. A string literal in an
extern inline function is the same object in different translation units."
That seems difficult for the compiler/
linker to insure, since multiple copies of the function get() will be
generated, but it's probably what the code intends, right? I'm asking
because I know of definite examples where a particular version of g++
breaks on similar code.
This particular version of g++ is not alone, see e.g. a case description,
where the MS VS2003 compiler can't handle this properly:
http://www.codeproject.com/useritems/VC2003MeyersSingletonBug.asp
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]