On Sunday, 22 October 2023 at 00:50:33 UTC, Jonathan M Davis
wrote:
Can I say in the general sense that when the word static is
used it means that something is defined/declared at compile
time?
Hmmm. It seems like my message got eaten. So, I'll write it out
again.
In any case, no, in general, static really doesn't have much to
with runtime vs compile time. It means different things in
different contexts. Off the top of my head, the only contexts
where static specifically has anything to do with compile time
are with static if and static foreach, in which case, those
constructs become compile-time constructs instead of runtime
constructos. Other contexts have very different meanings for
static.
For instance, a static member function is a member function
that doesn't have an implicit this reference/pointer and thus
is pretty much just a function that's scoped to the
class/struct rather than being a function that operates on
instances of that class or struct.
On the other hand, static member variables are variables which
are associated with the class or struct and not with an
instance of that class or struct. So, there is only one
instance of that variable for all objects of that class or
struct on a single thread, as opposed to non-static member
variables which are specific to each object.
static in functions has similar but different meanings. On a
nested function, it makes it so that the function has no
implicit parameter which is a reference to the context of the
outer function, meaning that it's pretty much just a function
within another function, whereas a non-static nested function
actually has access to the outer function's scope and thus can
access the variables in the outer scope.
On the other hand, a static variable within a function is a
variable where there is only one instance of that variable for
every call to that function on a single thread, as opposed to
normal function variables which get a new instance every time
that the function is called.
And there are other meanings for static in other contexts.
There are similarities between them, but if there is a
definition that can be given for what static means which covers
all of those contexts (and there may be - C manages that in
spite of the fact that static means very different things in
different contexts there too), it's not an obvious definition.
You mostly just have to learn what static means in each context
that it's used rather than memorizing a general definition for
it that can be applied in each context.
- Jonathan M Davis
Very helpful. Thanks Jonathan.
Wow. Sorry. I just found this unsent draft!