Why are anonymous namespaces not a sufficient replacement for namespace-static, according to the standards committee?

前端 未结 3 438
陌清茗
陌清茗 2020-12-07 12:31

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 13:04

    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.

    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.

提交回复
热议问题