Victor Bazarov <v.********@comacast.net> wrote:
"Scott J. McCaughrin" <sj******@bluestem.prairienet.org> wrote...
: > Victor Bazarov <v.********@comacast.net> wrote:
: > : Scott J. McCaughrin wrote:
: > : >
(intermediate commentary elided for brevity)
: > : > Are you sure this is appropriate, making a private member
: > : > "just like a global"?
: It's not just "appropriate", it's required. Every static data member
: shall be defined at the namespace level if it's used in the program
: _outside_ the class definition or if it's of non-integral type (or
: both).
(more intermediate responses frome me deleted for brevity)
: The only source of information I have about the language specification is
: the C++ language International Standard. In 9.4.2, paragraph 2, it says,
: "The definition for a static data member shall appear in a namespace scope
: enclosing the member's class definition." To me "shall" means "required".
Victor:
Thank you for citing the exact reference in the Standard, as it does shed
light on my concern. Our exchange went:
: > : > Are you sure this is appropriate, making a private member
: > : > "just like a global"?
: It's not just "appropriate", it's required.
Yet, note omssion of the term "global" from your citation, which tells me
that the Standard does not require static data members to reside in auto
storage, thus permitting them to reside in static storage as I maintained.
I suspect some of the confusion in this thread may arise from differing
interpretations of that term: "required". You take it to mean whatever
is stipulated by the Standard, while I read it to convey whatever a
compiler was capable of implementing.
: > But "information hiding" here refers to security against unintended
: > access to private members (static or otherwise). If I have to declare
: > a static member at global level, here is what can happen:
: >
: > Funct array[5];
: > Funct VarArray::funct; // a private static member of VarArray
: >
: > then: array[5] = (Funct)NULL; // unintended access to private member
: What? Huh? What unintended access? This code has _undefined_ behaviour.
: There can be no "intent" here.
Correct, which is why I said "unintended access". The behavior is indeed
"undefined" since alignment and padding considerations (if any) make it
so. But it is the _possibility_ of such access that worries me. Since
`array[]' and `funct' are of the same type, it is indeed possible that
the five elements array[0] ... array[4] can immediately precede `funct'
in auto store, so that array[5] coincides with `funct' and thereby
allow the spurious overwrite. Now, I readily concede that consigning
the private static member `funct' to static store does not guarantee
safety, but it can lessen the likelihood in cases where the static
store is treated less casually than the stack is. That may be the
best we can ever hope for.
: > Think about that.
: Nothing to think about. Undefined behaviour is undefined behaviour.
: If you want to convince anybody that there can be something _definite_
: about the code you presented here, sorry, you're mistaken.
But I am not trying to do that: only to show the possibility. Where did
I claim this would "definitely" happen, Victor? Indeed, if there is any
misperception of definiteness at all, it may accrue from use of the term
"private" to suggest that such data members are safe from unwarranted
intrusion, when my example was only submitted to assail that contention.
To put these matters into perspective, you may recall that my original
post dealt with the compiler (GNU gpp) complaining about my static data
member as an "undefined reference" (although I suspect it was the linker
complaining). Well, that means that the symbol for my static data member
could not be resolved to any kind of address, i.e., no storage had been
set aside for it, despite my declaration of it in the class as a static.
Thus, it was treating the symbol like an `extern' (I suspect other
loaders might have even given me "undefined extern" as a more inform-
ative diagnostic).
I do not agree that the user should have to define storage for a class-
level static member separate from its declaration, and so I submitted
my suggestion that -- like any non-class static variable -- it be
allocated in the static store implicity by the compiler when it first
encounters the symbol. Thus the symbol does have a memory address to
satisfy the linker.
I only raised the "vtbl" issue by way of showing how the compiler can
automatically define the static data member in static store without the
necessity of an explicit definition from the user separate from his/her
delcaration in its class. I am quite sure others can suggest far better
examples to illustrate the feasibility of achieving such compiler def-
inition. It should now only remain for the user to set values for the
static member at their discretion.
-- Scott