This question already has an answer here:
As we know, the keyword static
has multiple meanings in C. C99 added the possibility of legally writing
void foo (int arr[static 50])
{
// ...
}
which adds to the confusion, and C++ has static member variables and functions.
This would not be so troublesome if all the uses could be connected in some way, but I find it hard to find that link for some of the cases. Particularly why the static
keyword should be used to modify visibility (linkage), or what on earth it's got to do with an array's minimum amount of elements.
So is there a historical reason for the abuse of the static
keyword, or is there a secret link under the hood that connects all of its uses?
static
's original meaning in C++ is actually deprecated, replaced with unnamed namespaces. The only waystatic
is actually used in current C++ code is to be non-member.A camel is a horse designed by committee.
http://en.wikipedia.org/wiki/Design_by_committee
ADDED: Committee members involved in the design are conservative, and are more interested in not breaking existing C++ code than the potential elegance of new code.
There is a very simple way of remembering of all 3 C++ meanings of
static
I'm aware of.static
means "pretty much like global variable/function but only available directly in scope of..."struct
). This case is slightly different as you can access the variable/function through an object or by prefixing, but this is a little like asking the class or its object to provide you access to it, and it in fact can be denied (withprivate
). So the access isn't "direct".In case of the presented C99 array syntax, this is something completely different and I assume it was there to not introduce new keywords, as others suggest.
I think the reasons are different for the different usages that this keyword has. If we take the function scope and file scope use as of classical C for granted (they are at least similar concepts) the first addition off topic is the
static
in C++ to name a global member of a class.I guess here the shortcut was just that "static" and "global" seemed to be close enough and early C++ was very careful not to introduce new keywords that would break existing code. So they took an existing one that could not appear in that context.
For the C99 add-on for array parameters things are different, I think, because
static
is not the only addition, here. You may also have type qualifiers (const
andvolatile
) that qualify the implicit pointer:define compatible prototypes. I can only speculate that the choice of the storage class specifier
static
for the purpose that you mention (minimum length of the array) was seen as a natural extension of that syntax. Also probably it easily proved that this use was compatible with the rest of language, by arguing where a type qualifier may be used to extend the language, a storage class specifier can't do much harm.Adding new keywords to a language breaks backwards compatibility. So
static
gets used where its use might possibly mean something (int arr[static 50]
vsint arr[auto 50]
orint arr[extern 50]
) and cannot syntactically appear in that location based its use in previous versions.Though in that case adding a
not_less_than
context sensitive keyword in that position would not break previous code, it would add another keyword (so simple text editors which are keyword aware but not syntax aware would not know whether or not it is a keyword), and break the 'keywords are not context sensitive' simplification made in C.