Is it legal C++ to declare a nested namespace `std`?

ⅰ亾dé卋堺 提交于 2020-01-03 12:33:28

问题


The std namespace is special in C++, so ...

Is this legal C++?

// at global scope
namespace mine {
  namespace std {
    ...
  }
}

I'd call it insane, but is it allowed?

A reference (or non-reference) from the Standard would be appreciated.


回答1:


In the reserved names standard 17.4.3.1 (and its sub-paragraphs) I can't find anything that prohibits using std as a nested namespace name. It's not a macro, it's not in the global namespace, and it doesn't seem to meet any of the "external linkage criteria" that would prohibit it.

It appears to be legal (although as you note extremely inadvisable).




回答2:


The rule in the standard that makes the std namespace "special" is (§17.6.4.2.1 [namespace.std]/p1):

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified.

This only applies to the top level namespace.



来源:https://stackoverflow.com/questions/25169745/is-it-legal-c-to-declare-a-nested-namespace-std

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!