Article View: comp.std.c++
Article #2438Re: ??? static locals in inline functions ???
From: Fergus Henderson
Date: Mon, 06 Nov 1995 10:52
Date: Mon, 06 Nov 1995 10:52
65 lines
2425 bytes
2425 bytes
kaelin@bridge.com (Kaelin Colclasure) writes:
>What behavior does the draft standard condone for the following
>scenario:
>
>1) In a header file, an inline function is defined to contain a local
>static, like so:
>
> inline int *
> foo()
> {
> static int i;
> return &i;
> }
>
>2) Two seperate modules include this header file, call them A and B.
>A "initializes" foo():
>
> #include "foo.h"
> ...
> *foo() = 1;
> ...
>
>3) B calls foo(), dereferences the pointer returned and prints its
>value:
>
> #include <iostream.h>
> #include "foo.h"
> ...
> cerr << "*foo() = " << *foo() << endl;
> ...
>
>Some data points:
>
>MSVC++: *foo() = 1
>G++: *foo() = 0
>Sun C++:*foo() = 0
>CFront: "sorry, not implemented"
>
>My intuition is that MSVC++ is doing the "right" thing (gasp), and what
>"should" be the standard thing-- but I realize that the behavior I
>want requires mangling a global variable name or the moral
>equivalent. I want to know if the standard requires this or leaves
>the behavior implementation defined...
The standard mandates the behaviour of g++ and Sun C++.
The reason is that an inline function by default has static linkage,
and so each translation unit gets its own copy.
To get the behaviour exhibited by MSVC++ from a draft
standard-conforming compiler, you would have to explicitly declare foo() as
`extern'. That is, you would have to change the definition of foo() from
inline int * foo() { ... }
to
extern inline int * foo() { ... }
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<9530806.23850@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<KAELIN.95Nov2201050@binki.bridge.com>Show Full Headers
Complete Article Headers
Approved: fjh@cs.mu.oz.au Date: 06 Nov 95 10:52:02 GMT From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson) Message-ID: <9530806.23850@mulga.cs.mu.OZ.AU> Newsgroups: comp.std.c++ Organization: Computer Science, University of Melbourne, Australia Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back References: <KAELIN.95Nov2201050@binki.bridge.com> Subject: Re: ??? static locals in inline functions ??? X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJ3o6OEDnX0m9pzZAQH8sQGAkQCJ2XoT5MRCjeSuVO64gar3bBNrbTOa IaRJL5CT7gnZon3PAqsXJaEoRjxQl8Ns =EMs+