Topic: Statics and in-lines
Author: carson@gwis2.circ.gwu.edu (John Carson)
Date: 1995/04/28 Raw View
John Max Skaller (maxtal@Physics.usyd.edu.au) wrote:
: In article <3nb5vm$4au@cronkite.seas.gwu.edu>,
: John Carson <carson@gwis2.circ.gwu.edu> wrote:
: >I suspect this has been discussed before but I believe it is not valid to
: >define an in-line function that contains a static. Am I correct and for
: >purposes of learning, where would one find the answer in the ARM or draft
: >standard?
: It is valid to have local static variables in any function.
: If the function is inline or inclass, then:
: 1) If the function has internal linkage then each translation
: unit has a distinct function and the local statics are distinct
: 2) If the function has external linkage then there is only
: one function in any translation unit and only one static
: variable.
: FYI: these rules are not in the ARM. A new rule you should know
: about is that ALL non-local classes effectively have external linkage
: and ALL member functions of such classes also (effectively) have external
: linkage.
: That is, effectively linkage is "inherited" by all
: nested entities. This is NOT true for inheritance by the way,
: in fact the reverse is true: a class derived from a class
: with external linkage can have internal linkage BUT a class
: derived from a class with internal linkage must have internal
: linkage. (This can happen only if the derived class is local
: to a function)
: It isn't clear the WP correctly states these rules yet.
: --
: JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
: Maxtal Pty Ltd,
: 81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
: NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Wow,
I think it is easier to build a dummy class that contains a static and
then make a friend function for it to accomplish the desired result.
John C.
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/04/27 Raw View
carson@gwis2.circ.gwu.edu (John Carson) writes:
>I suspect this has been discussed before
Yes, at least twice before...
>but I believe it is not valid to
>define an in-line function that contains a static.
It is valid, but it may not do what you expect, because
inline functions have static linkage (unless otherwise specified),
and if the "same" static function is defined in more than one
translation unit, they get separate copies any the static variables.
If the inline function has external linkage (e.g. if it is a member
function), then in theory it should be fine. In practice some
(low-quality) compilers may fail to inline it, and it is possible
that some buggy compilers might even generate incorrect code for it.
>Am I correct and for
>purposes of learning, where would one find the answer in the ARM or draft
>standard?
Check WP 3.5 (Program and linkage).
--
Fergus Henderson | Tell you what: go write a 100x100 matrix multiply
fjh@cs.mu.oz.au | of integers in both languages and then let's talk
http://www.cs.mu.oz.au/~fjh | about speed, ok? - Tom Christiansen.
Author: joshua@oncomdis.on.ca (joshua)
Date: 1995/04/24 Raw View
John Carson (carson@gwis2.circ.gwu.edu) wrote:
: I suspect this has been discussed before but I believe it is not valid to
: define an in-line function that contains a static. Am I correct and for
: purposes of learning, where would one find the answer in the ARM or draft
: standard?
Let's put it this way..
you can declare a function to be inline that uses a static variable
but the compiler will probably ignore the request to inline the
function.
conclusion: it's legal but most likely not valid.
-- Joshua Allen
Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/04/24 Raw View
In article <3nb5vm$4au@cronkite.seas.gwu.edu>,
John Carson <carson@gwis2.circ.gwu.edu> wrote:
>I suspect this has been discussed before but I believe it is not valid to
>define an in-line function that contains a static. Am I correct and for
>purposes of learning, where would one find the answer in the ARM or draft
>standard?
It is valid to have local static variables in any function.
If the function is inline or inclass, then:
1) If the function has internal linkage then each translation
unit has a distinct function and the local statics are distinct
2) If the function has external linkage then there is only
one function in any translation unit and only one static
variable.
FYI: these rules are not in the ARM. A new rule you should know
about is that ALL non-local classes effectively have external linkage
and ALL member functions of such classes also (effectively) have external
linkage.
That is, effectively linkage is "inherited" by all
nested entities. This is NOT true for inheritance by the way,
in fact the reverse is true: a class derived from a class
with external linkage can have internal linkage BUT a class
derived from a class with internal linkage must have internal
linkage. (This can happen only if the derived class is local
to a function)
It isn't clear the WP correctly states these rules yet.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: carson@gwis2.circ.gwu.edu (John Carson)
Date: 1995/04/22 Raw View
I suspect this has been discussed before but I believe it is not valid to
define an in-line function that contains a static. Am I correct and for
purposes of learning, where would one find the answer in the ARM or draft
standard?
Thanks,
P.S. I know how to make the equivalent with a class and static data
member and inline friend function.