Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 510
Martin Ba Avatar asked Aug 06 '14 20:08

Martin Ba


Video Answer


2 Answers

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).

like image 64
Mark B Avatar answered Sep 27 '22 15:09

Mark B


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.

like image 27
T.C. Avatar answered Sep 27 '22 17:09

T.C.