C++ 弃用 static 关键字...不再使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4726570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deprecation of the static keyword... no more?
提问by Matthieu M.
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).
在 C++ 中,可以static
在翻译单元中使用关键字来影响符号的可见性(变量或函数声明)。
In n3092, this was deprecated:
在 n3092 中,这已被弃用:
Annex D.2 [depr.static]
The use of the static keyword is deprecated when declaring objects in namespace scope (see 3.3.6).
附件 D.2 [depr.static]
在命名空间范围内声明对象时不推荐使用 static 关键字(见 3.3.6)。
In n3225, this has been removed.
在 n3225 中,这已被删除。
The only article I could findis somewhat informal.
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 兼容(以及将 C 程序编译为 C++ 的能力),弃用令人讨厌。但是,将 C 程序直接编译为 C++ 已经是一种令人沮丧的经历,因此我不确定是否值得考虑。
Does anyone know why it was changed ?
有谁知道为什么改了?
采纳答案by Johannes Schaub - litb
In C++ Standard Core Language Defect Reports and Accepted Issues, Revision 94under 1012. Undeprecating static` they note:
在C++ Standard Core Language Defect Reports and Accepted Issues, Revision 94under 1012. Undeprecating 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.
尽管 7.3.1.1 [namespace.unnamed] 声明不推荐使用 static 关键字在命名空间范围内声明变量,因为未命名命名空间提供了更好的选择,但在可预见的未来,该功能不太可能被删除.
Basically saying that the deprecation of static
doesn't really make sense. It won't ever be removed from C++, and it's still useful because you don't need the boilerplate code you need with unnamed namespaces, if you just want to declare a function or object with internal linkage.
基本上说弃用 的static
意义不大。它永远不会从 C++ 中删除,并且它仍然很有用,因为如果您只想声明具有内部链接的函数或对象,则不需要带有未命名名称空间的样板代码。
回答by curiousguy
I will try to answer your question, although it is an old question, and it does not look very important (it really is not very important in itself), and it has received quite good answers already. The reason I want to answer it is that it relates to fundamental issues of standard evolution and language design when the language is based on an existing language: when should language features be deprecated, removed, or changed in incompatible ways?
我会尽量回答你的问题,虽然它是一个老问题,看起来不是很重要(它本身确实不是很重要),它已经得到了很好的答案。我想回答它的原因是,当语言基于现有语言时,它涉及标准演变和语言设计的基本问题:何时应该以不兼容的方式弃用、删除或更改语言特性?
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).
在 C++ 中,可以在翻译单元中使用 static 关键字来影响符号的可见性(变量或函数声明)。
The linkage actually.
其实联动。
In n3092, this was deprecated:
在 n3092 中,这已被弃用:
Deprecation indicates:
弃用表示:
- The intentto remove some feature in the future; this does not mean that deprecated features will be removed in the next standard revision, or that they must be removed "soon", or at all. And non-deprecated features may be removed in the next standard revision.
- A formal attempt to discourage its use.
- 将来删除某些功能的意图;这并不意味着不推荐使用的功能将在下一个标准修订版中删除,或者它们必须“很快”删除,或者完全删除。未弃用的功能可能会在下一个标准修订版中删除。
- 正式尝试阻止其使用。
The latter point is important. Although there is never a formal promise that your program won't be broken, sometimes silently, by the next standard, the committee should try to avoid breaking "reasonable" code. Deprecation should tell programmers that it is unreasonable to depend on some feature.
后一点很重要。虽然从来没有正式承诺你的程序不会被破坏,有时是默默地,按照下一个标准,委员会应该尽量避免破坏“合理”的代码。弃用应该告诉程序员依赖某些功能是不合理的。
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 兼容(以及将 C 程序编译为 C++ 的能力),弃用令人讨厌。但是,将 C 程序直接编译为 C++ 已经是一种令人沮丧的经历,因此我不确定是否值得考虑。
It is very important to preserve a C/C++ common subset, especially for header files. Of course, static
global declarations are declarations of symbol with internal linkage and this not very useful in a header file.
保留 C/C++ 公共子集非常重要,尤其是头文件。当然,static
全局声明是具有内部链接的符号声明,这在头文件中不是很有用。
But the issue is never just compatibility with C, it's compatibility with existing C++: there are tons of existing valid C++ programs that use static
global declarations. This code is not just formally legal, it is sound, as it uses a well-defined language feature the way it is intended to be used.
但问题不仅仅是与 C 的兼容性,而是与现有 C++ 的兼容性:有大量现有的有效 C++ 程序使用static
全局声明。这段代码不仅在形式上合法,而且很合理,因为它按照预期使用的方式使用了定义良好的语言功能。
Just because there is now a "better way" (according to some) to do something does not make the programs written the old way "bad" or "unreasonable". The ability of using the static
keyword on declarations of objects and functions at global scope is well understood in both C and C++ communities, and most often used correctly.
仅仅因为现在有一种“更好的方法”(根据某些人的说法)来做某事并不会使以旧方式编写的程序“糟糕”或“不合理”。static
在 C 和 C++ 社区中对全局范围内的对象和函数声明使用关键字的能力很好理解,并且最常正确使用。
In a similar vein, I am not going to change C-style casts to double
to static_cast<double>
just because "C-style casts are bad", as static_cast<double>
adds zero information and zero safety.
同样,我不会仅仅因为“C 风格的强制转换不好”而将 C 风格的强制转换更改double
为static_cast<double>
,因为static_cast<double>
增加了零信息和零安全性。
The idea that whenever a new way to do something is invented, all programmers would rush to rewrite their existing well-defined working code is just crazy. If you want to remove all the inherited C ugliness and problems, you don't change C++, you invent a new programming language.Half-removing one use of static
hardly makes C++ less C-ugly.
每当发明了一种新的做事方式时,所有程序员都会急于重写他们现有的定义良好的工作代码的想法是疯狂的。如果你想去除所有继承来的 C 丑陋和问题,你不改变 C++,你发明了一种新的编程语言。半删除一次使用static
几乎使 C++ 不那么 C 丑陋。
Code changes need a justification, and "old is bad" is never a justification for code changes.
代码更改需要理由,“旧的不好”永远不是代码更改的理由。
Breaking language changes need a very strong justification. Making the language very slightly simpler is never a justification for a breaking change.
打破语言变化需要一个非常有力的理由。使语言稍微简单一点绝不是进行重大更改的理由。
The reasons given why static
is bad are just remarkably weak, and it isn't even clear why not both objects and function declarations are deprecated together - giving them different treatment hardly makes C++ simpler or more orthogonal.
给出的为什么static
不好的原因非常薄弱,甚至不清楚为什么不推荐同时弃用对象和函数声明 - 给予它们不同的处理很难使 C++ 更简单或更正交。
So, really, it is a sad story. Not because of the practical consequences it had: it had exactly zero practical consequences. But because it shows a clear lack of common sense from the ISO committee.
所以,真的,这是一个悲伤的故事。不是因为它有实际后果:它的实际后果完全为零。但因为它表明 ISO 委员会明显缺乏常识。
回答by Maxim Egorushkin
Deprecated or not, removing this language feature would break existing codes and annoy people.
无论是否弃用,删除此语言功能都会破坏现有代码并惹恼人们。
The whole static deprecation thing was just wishful thinking along the lines of "anonymous namespaces are better than static" and "references are better pointers". Lol.
整个静态弃用的事情只是“匿名命名空间比静态更好”和“引用是更好的指针”的一厢情愿。哈哈。