0

According to the (now defunct) StackOverflow documentation on Extending the std namespace and [namespace.constraints]:

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.

and

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace posix or to a namespace within namespace posix unless otherwise specified. The namespace posix is reserved for use by ISO/IEC 9945 and other POSIX standards.

Do these rules only apply to the global std and posix namespaces, or is something the following also undefined:

namespace HelloWorld {
    namespace std {
        void terminate();
    }
}

Perhaps it's because I'm native in English, but the wording of those rules seems to be a bit vague in this aspect.

Jonathan Magnan
  • 10,874
  • 2
  • 38
  • 60
jotik
  • 17,044
  • 13
  • 58
  • 123
  • 2
    The rule only apply to the top-level namespaces. But using a namespace named e.g. `std` inside another namespace will be confusing for the people reading or maintaining your code (which might include you in the future). Think about what happens if you do `using namespace HelloWorld` (with the example above). – Some programmer dude Feb 02 '17 at 10:24
  • Not really, it's not extending `std` since it's done in a different namespace. And it will also cause massive problems doing something like that. While there is no rule forbidding the use of the name `std` inside another namespace (that I know of) it's not something you should do, or even consider doing. – Some programmer dude Feb 02 '17 at 10:35
  • Also related: [On namespace 'names': ::std:: vs std::](http://stackoverflow.com/q/18544651/3919155). – jotik Feb 02 '17 at 14:13

1 Answers1

2

It's indeed a somewhat obscure rule of English. You see that the rules talk about a namespace within namespace std. Why aren't we talking about "a namespace std" or "the namespace std"? It's because std is the (unique) name of that namespace. Compare "a president", "the president", "president Trump".

So yes, namespace std uniquely refers to that top-level namespace.

MSalters
  • 173,980
  • 10
  • 155
  • 350