Arne
Arne

Reputation: 2674

Why do I have to specify the outer namespace for a function in a nested anonymous namespace?

Given a namespace A. Inside is an anonymous namespace with function f and a class X, also with function f: Why do I have to specify the outer namespace A:: as a qualifier when calling anonymous f from A::X::f?

As a minimal example:

#include <iostream>

using namespace std;

namespace A {

 namespace {
     int f( int i ) { return i; }
 }

 class X {
 public:
     static int f() { A::f( 10 ); }
 };

}


int main()
{
   cout << A::X::f() << endl; 

   return 0;
}

Upvotes: 2

Views: 179

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254701

Because, within the scope of X::f, the unqualified name f refers to X::f, not any other f. A name declared within a scope will hide anything with the same name in an outer scope.

Upvotes: 7

Related Questions