Warm tip: This article is reproduced from serverfault.com, please click

If Namespaces can only have a Global Scope how can there be Nested Namespaces?

发布于 2020-12-01 11:41:25

I read these 2 pieces of codes and their descriptions and thought that they were clashing with each other.

  1. Namespace definition can appear only in a global scope.
void f()
{
    namespace space1{

    }
}
// So this is not allowed as space1 is local to f()
  1. Namespaces can be nested
namespace namespace1{
    int i;
    namespace namespace2{
       int j;
    }
}

In this case won't namespace2 be local to namespace1 thus resulting in an error?

Questioner
Mihir
Viewed
0
Eric 2020-12-01 19:48:26

Because your quote is wrong. A more correct description would be:

Namespace definitions are only allowed at namespace scope, including the global scope.