为什么匿名命名空间不能替代命名空间 - 静态,根据标准委员会? [英] Why are anonymous namespaces not a sufficient replacement for namespace-static, according to the standards committee?

查看:123
本文介绍了为什么匿名命名空间不能替代命名空间 - 静态,根据标准委员会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此答案,命名空间范围的静态变量在C ++ 11中未被淘汰。也就是说,它们在C ++ 03中已被弃用,因为匿名命名空间被认为更好。但是C ++ 11没有推翻它们。

According to this answer, namespace-scoped static variables were undeprecated in C++11. That is, they were deprecated in C++03, because anonymous namespaces were considered better. But C++11 undeprecated them.

为什么? N3296将此原因列为:


在命名空间范围中使用静态不应被弃用。匿名命名空间不能充分代替功能。

The use of static in namespace scope should not be deprecated. Anonymous namespaces are not a sufficient replacement for the functionality.

这显然被委员会接受。为什么?

This was apparently accepted by the committee. Why? What is it about anonymous namespaces that does not completely replace this functionality?

我更喜欢那些有标准委员会讨论的文件或文件的回答。

I would prefer answers that had some documentation or paper trail of a standards committee discussion.

推荐答案

这是一个更深入的解释。


虽然7.3.1.1 [namespace.unnamed]使用static关键字在命名空间范围中声明变量已被弃用,因为未命名的命名空间提供了一个更好的选择,在可预见的未来,特征将不可能在任何时候被删除,特别是考虑到C兼容性问题。

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.

我知道的一个问题是,匿名命名空间不能专门化命名空间块之外的模板。这就是为什么引入 inline命名空间,虽然 static 也有效。此外, static 在宏中播放的效果非常好。

One issue I know is that anonymous namespaces can't specialize templates outside of the namespace block. This is why inline namespace was introduced, although static works too. Also, static plays much nice with macros.

这篇关于为什么匿名命名空间不能替代命名空间 - 静态,根据标准委员会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆