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

2023-12-02C/C++开发问题
1

本文介绍了为什么“静态"关键字在 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.

这篇关于为什么“静态"关键字在 C 和 C++ 中有这么多含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6