为什么“静态”会发生?关键字在 C 和 C++ 中有这么多含义?
众所周知,关键字 static
具有多重含义< /a> 在 C 中。C99 添加了合法编写的可能性
void foo (int arr[static 50])
{
// ...
}
,这增加了混乱,并且 C++ 具有静态成员变量和函数。
如果所有用途都可以以某种方式连接起来,这不会那么麻烦,但我发现很难找到某些情况下的链接。特别是为什么应该使用static
关键字来修改可见性(链接),或者它到底与数组的最小元素数量有关。
那么,滥用 static
关键字是否有历史原因,或者幕后是否存在连接其所有用途的秘密链接?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
向语言添加新关键字会破坏向后兼容性。因此,
static
会在其使用可能意味着某种含义的地方使用(int arr[static 50]
与int arr[auto 50]
或int arr[extern 50]
),并且根据其在以前版本中的使用,在语法上不能出现在该位置。虽然在这种情况下,在该位置添加一个
not_less_than
上下文敏感关键字不会破坏以前的代码,但它会添加另一个关键字(因此,识别关键字但不识别语法的简单文本编辑器将不知道是否它是一个关键字),并打破了 C 中的“关键字不是上下文相关的”简化。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.有一种非常简单的方法可以记住我所知道的
static
的所有 3 个 C++ 含义。static
表示“非常类似于全局变量/函数,但仅在...范围内直接可用”“struct
声明的类)。这种情况略有不同,因为您可以通过对象或前缀访问变量/函数,但这有点像要求类或其对象为您提供对它的访问,实际上它可以被拒绝(使用私有
)。所以访问不是“直接”的。对于所提供的 C99 数组语法,这是完全不同的东西,我认为它不会像其他人建议的那样引入新的关键字。
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.
static
在 C++ 中的原始含义实际上已被弃用,取而代之的是未命名的命名空间。当前 C++ 代码中实际使用static
的唯一方式是成为非成员。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.我认为这个关键字的不同用法的原因是不同的。如果我们认为经典 C 中的函数作用域和文件作用域的使用是理所当然的(它们至少是相似的概念),那么第一个偏离主题的添加是 C++ 中的
static
,用于命名类的全局成员。我想这里的捷径只是“静态”和“全局”似乎足够接近,早期的 C++ 非常小心,不引入会破坏现有代码的新关键字。因此,他们采用了一个无法出现在该环境中的现有方案。
我认为,对于数组参数的 C99 附加组件,情况有所不同,因为
static
并不是这里唯一的附加项。您还可以使用类型限定符(const
和volatile
)来限定隐式指针:定义兼容的原型。我只能推测,出于您提到的目的(数组的最小长度)而选择存储类说明符
static
被视为该语法的自然扩展。也可能很容易证明这种用法与语言的其余部分兼容,通过争论类型限定符可以用于扩展语言,存储类说明符不会造成太大伤害。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.骆驼是委员会设计的马。
http://en.wikipedia.org/wiki/Design_by_committee
添加:
参与设计的委员会成员比较保守,他们更关心的是不破坏现有的 C++ 代码,而不是新代码的潜在优雅性。
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.