Namespace Access Rules

I was browsing section 7.3.1.1 in the C ++ 03 standard, expecting to find some description of access rules for elements defined in an unnamed namespace.

The rules appear to be slightly different for unnamed namespaces, since you cannot fully qualify access to elements in one. I know that, at least within the same translation unit, you can access elements in an unnamed namespace, as if they were not in the namespace. For instance:

namespace {
  int foo;
}

void something()
{
  foo = 4;
}

If the namespace has a name, you could not do this. So, where are the rules defined in the standard for these exceptional rules that apply to unnamed namespaces?

+5
2

:

namespace unique_per_TU
{
    // Stuff
}
using namespace unique_per_TU;

.

EDIT:

, 7.3.1.1/1

,

namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespacebody }

, .

"" , .

+6

, 7.3.1.1/1,

3.3.5/1 :

- . , -namespace-name, , , . , , , , , . . (3.3.1) ; - (7.3.4), , -, - .

>[Example:
namespace N {
      int i;
      int g(int a) { return a; }
      int j();
      void q();
}
namespace { int l=1; }
// the potential scope of l is from its point of declaration
// to the end of the translation unit

namespace N {
    int g(char a) // overloadsN::g(int)
    {
        return l+a; // l is from unnamed namespace
    }
    int i; // error: duplicate definition
    int j(); // OK: duplicate function declaration
    int j() // OK: definition ofN::j()
    {
       return g(i); // callsN::g(int)
    }
    int q(); // error: different return type
}

-end ]

:
l

+2

All Articles