Why does the “static” keyword have so many meaning

2019-01-17 10:31发布

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?

5条回答
仙女界的扛把子
2楼-- · 2019-01-17 10:49

static's original meaning in C++ is actually deprecated, replaced with unnamed namespaces. The only way static is actually used in current C++ code is to be non-member.

查看更多
Rolldiameter
3楼-- · 2019-01-17 10:59

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.

查看更多
女痞
4楼-- · 2019-01-17 11:03

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..."

  • "...this file" if it is in global scope.
  • "...this function" if it is in a function (including member functions). Note that if you make classes and lambdas in a function, they are still in this scope. Lambda with an empty capture can access static variable of its "parent" function.
  • "...this class" if it is in a class (including those declared with 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 (with private). 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.

查看更多
不美不萌又怎样
5楼-- · 2019-01-17 11:03

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 and volatile) that qualify the implicit pointer:

void toto1(char str[const 5]);
void toto2(char*const str);

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.

查看更多
小情绪 Triste *
6楼-- · 2019-01-17 11:07

Adding new keywords to a language breaks backwards compatibility. So static gets used where its use might possibly mean something ( int arr[static 50] vs int arr[auto 50] or int 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.

查看更多
登录 后发表回答