jotik jotik - 3 years ago 127
C++ Question

Is using nested std and posix namespaces undefined?

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download