c++ - 根据标准委员会的说法,为什么匿名命名空间不足以替代命名空间静态?

标签 c++ c++11

根据this answer , 命名空间范围的静态变量在 C++11 中未被弃用。也就是说,它们在 C++03 中已被弃用,因为匿名命名空间被认为更好。但是 C++11 不推荐使用它们。

为什么? N3296 lists the reasoning for this如:

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

这显然被委员会接受了。为什么?没有完全取代这个功能的匿名命名空间是什么?

我更喜欢有标准委员会讨论的文档或书面记录的答案。

最佳答案

This is a more in-depth explanation.

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.

我知道的一个问题是匿名命名空间不能专门化命名空间 block 之外的模板。这就是引入 inline namespace 的原因,尽管 static 也可以。此外,static 与宏配合得很好。

关于c++ - 根据标准委员会的说法,为什么匿名命名空间不足以替代命名空间静态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8460327/

相关文章:

c++ - 默认情况下是否创建 move 构造函数?

c++ - 转换宏参数名称以用作 C++ 中的函数

c++ - 如何删除字符串 vector 中的字符?

c++ - C++ 中的 'override' 关键字是做什么用的?

c++ - 带按值参数 & noexcept 的构造函数

c++ - 什么是 std::views::counted?

c++ - 按值而不是键对映射进行排序

c++ - 是否可以使用索引迭代器访问另一个数组的索引?

c++ - 不可用的 Ui 属性

c++ - 如何获得两个(伪)随机但彼此不同的容器迭代器/元素?