为什么“静态"关键字在 C 和 C++ 中有这么多含义?
我们知道,关键字 static
具有 C99中的多重含义增加了合法写的可能性
As we know, the keyword static
has multiple meanings in C. C99 added the possibility of legally writing
void foo (int arr[static 50])
{
// ...
}
这增加了混乱,而 C++ 具有静态成员变量和函数.
which adds to the confusion, and C++ has static member variables and functions.
如果可以以某种方式连接所有用途,这不会那么麻烦,但我发现在某些情况下很难找到该链接.特别是为什么应该使用 static
关键字来修改可见性(链接),或者它到底与数组的最小元素量有什么关系.
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.
那么,static
关键字的滥用是否有历史原因,或者其背后是否有一个秘密链接可以连接其所有用途?
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
在它的使用可能意味着什么的地方被使用( int arr[static 50]
vs int arr[auto 50]
或 int arr[extern 50]
) 并且不能根据其在以前版本中的使用在语法上出现在该位置.
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.
虽然在那种情况下,在那个位置添加一个 not_less_than
上下文敏感的关键字不会破坏以前的代码,它会添加另一个关键字(所以简单的文本编辑器知道关键字但不知道语法是否是关键字),并打破 C 语言中关键字不是上下文敏感"的简化.
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.
相关文章