为什么“静态”会发生?关键字在 C 和 C++ 中有这么多含义?

发布于 2024-10-11 03:32:24 字数 456 浏览 8 评论 0原文

众所周知,关键字 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

可遇━不可求 2024-10-18 03:32:24

向语言添加新关键字会破坏向后兼容性。因此,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] 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.

森林散布 2024-10-18 03:32:24

有一种非常简单的方法可以记住我所知道的 static 的所有 3 个 C++ 含义。 static 表示“非常类似于全局变量/函数,但仅在...范围内直接可用”“

  • ...此文件”(如果它在全局范围内)。
  • 如果它在函数(包括成员函数)中,则为“...此函数”。请注意,如果您在函数中创建类和 lambda,它们仍然在此范围内。具有空捕获的 Lambda 可以访问其“父”函数的静态变量。
  • “...这个类”如果它在一个类中(包括用 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..."

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

情仇皆在手 2024-10-18 03:32:24

static 在 C++ 中的原始含义实际上已被弃用,取而代之的是未命名的命名空间。当前 C++ 代码中实际使用 static 的唯一方式是成为非成员。

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.

暖伴 2024-10-18 03:32:24

我认为这个关键字的不同用法的原因是不同的。如果我们认为经典 C 中的函数作用域和文件作用域的使用是理所当然的(它们至少是相似的概念),那么第一个偏离主题的添加是 C++ 中的 static ,用于命名类的全局成员。

我想这里的捷径只是“静态”和“全局”似乎足够接近,早期的 C++ 非常小心,不引入会破坏现有代码的新关键字。因此,他们采用了一个无法出现在该环境中的现有方案。

我认为,对于数组参数的 C99 附加组件,情况有所不同,因为 static 并不是这里唯一的附加项。您还可以使用类型限定符(constvolatile)来限定隐式指针:

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

定义兼容的原型。我只能推测,出于您提到的目的(数组的最小长度)而选择存储类说明符 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 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.

硬不硬你别怂 2024-10-18 03:32:24

骆驼是委员会设计的马。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文