弃用 static 关键字...不再使用?

2022-01-05 00:00:00 static standards c++ c++11

在 C++ 中,可以在翻译单元中使用 static 关键字来影响符号(变量或函数声明)的可见性.

In C++ it is possible to use the static keyword within a translation unit to affect the visibility of a symbol (either variable or function declaration).

在 n3092 中,这已被弃用:

In n3092, this was deprecated:

附件 D.2 [depr.static]
在命名空间范围内声明对象时,不推荐使用 static 关键字(参见 3.3.6).

Annex D.2 [depr.static]
The use of the static keyword is deprecated when declaring objects in namespace scope (see 3.3.6).

在 n3225 中,这已被删除.

In n3225, this has been removed.

我能找到的唯一一篇文章有点不正式.

不过,它确实强调,为了与 C 兼容(以及将 C 程序编译为 C++ 的能力),弃用令人讨厌.但是,将 C 程序直接编译为 C++ 已经是一种令人沮丧的体验,因此我不确定是否值得考虑.

It does underline though, that for compatibility with C (and the ability to compile C-programs as C++) the deprecation is annoying. However, compiling a C program directly as C++ can be a frustrating experience already, so I am unsure if it warrants consideration.

有谁知道为什么要改?

推荐答案

在 C++ 标准核心语言缺陷报告和已接受的问题,修订版 94,1012.不赞成静态他们注意到:

In C++ Standard Core Language Defect Reports and Accepted Issues, Revision 94 under 1012. Undeprecating static they note:

尽管 7.3.1.1 [namespace.unnamed] 声明不推荐使用 static 关键字在命名空间范围内声明变量,因为未命名命名空间提供了更好的选择,但该功能不太可能在任何时候被删除可预见的未来.

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.

基本上这是说弃用 static 并没有真正的意义.它永远不会从 C++ 中删除.它仍然很有用,因为如果您只想声明具有内部链接的函数或对象,则不需要带有未命名 namespace 的样板代码.

Basically this is saying that the deprecation of static doesn't really make sense. It won't ever be removed from C++. It's still useful because you don't need the boilerplate code you would need with unnamed namespace's if you just want to declare a function or object with internal linkage.

相关文章