来自以下stackoverflow answer ,用户说:
It means that the variable is local to a translation unit (simply put, to a single source file), and cannot be accessed from outside it. This use of static is in fact deprecated in the current C++ Standard - instead you are supposed to use anonymous namespaces:
static int x = 0;
should be:
namespace { int x = 0; }
我不同意匿名命名空间是首选方法,
但是现在真的不推荐使用 static 了吗?
标准在哪里说的?
最佳答案
不,它目前没有被弃用。它曾一度出现,但由于 C 语言的可比性问题而被逆转。在 1999 年之前的某个时候它被弃用了,这导致了 defect report 174其中说:
The decision to deprecate global static should be reversed.
- We cannot deprecate static because it is an important part of C and to abandon it would make C++ unnecessarily incompatible with C.
- Because templates may be instantiated on members of unnamed namespaces, some compilation systems may place such symbols in the global linker space, which could place a significant burden on the linker. Without static, programmers have no mechanism to avoid the burden.
这导致 defect report 223其中弃用的含义修改自:
deprecated is defined as: Normative for the current edition of the Standard, but not guaranteed to be part of the Standard in future revisions.
有人指出,这意味着 future 的标准将只支持未弃用的功能:
However, this definition would appear to say that any non-deprecated feature is "guaranteed to be part of the Standard in future revisions." It's not clear that that implication was intended, so this definition may need to be amended.
并将已弃用的含义更改为:
These are deprecated features, where deprecated is defined as: Normative for the current edition of the Standard, but having been identified as a candidate for removal from future revisions.
后来,由于 C 兼容性问题,defect report 1012 不再推荐使用该功能。 :
Although 7.3.1.1 [namespace.unnamed] states that the use of the static keyword for declaring variables in namespace scope is deprecated because the unnamed namespace provides a superior alternative, it is unlikely that the feature will be removed at any point in the foreseeable future, especially in light of C compatibility concerns. The Committee should consider removing the deprecation.
关于c++ - 在确保翻译单元之间的可用性时是否弃用静态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34139882/