views:

101

answers:

4

Consider the following snippet:

void Foo() // 1
{
}

namespace
{
  void Foo() // 2
  {
  }
}

int main()
{
  Foo(); // Ambiguous.
  ::Foo(); // Calls the Foo in the global namespace (Foo #1).

  // I'm trying to call the `Foo` that's defined in the anonymous namespace (Foo #2).
}

How can I refer to something inside an anonymous namespace in this case?

+5  A: 

You can't. The standard contains the following section (§7.3.1.1, C++03):

An unnamed-namespace-definition behaves as if it were replaced by

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

where all occurrences of unique in a translation unit are replaced by the same identifier and this identifier differs from all other identifiers in the entire program.

Thus you have no way to refer to that unique name.

You could however technically use something like the following instead:

int i;

namespace helper {
    namespace {
        int i;
        int j;
    }
}

using namespace helper;

void f() { 
    j++; // works
    i++; // still ambigous
    ::i++; // access to global namespace
    helper::i++; // access to unnamed namespace        
}
Georg Fritzsche
Okay, what the standard says makes sense. However, why is there an ambiguity, then? If I can't access the Foo in the anonymous namespace, the compiler should be able to deduce that the only other Foo is the one in global scope. I shouldn't have to use the scope resolution operator at all. Any thoughts on that aspect of the problem?
sharp02
Re: your edit -- That's pretty much what I'm doing now, except I use a wrapper function to get to the actual target function.
sharp02
@sharp: There are two symbols `Foo` in that scope. Consider an equivalent situation: `namespace A { int i; } namespace B { int i; } using namespace A; using namespace B;`.
Georg Fritzsche
Good point, but that still seems kind of ugly from the language perspective. =\
sharp02
@sharp: Ugly is having two functions with the same name like that. :)
GMan
@GMan: Nah, [this](http://stackoverflow.com/questions/2781339/global-qualification-in-a-class-declarations-class-head) is ugly ;)
Georg Fritzsche
A: 

The only real way is to put the code you want to access that namespace within the namespace itself. There's no way to resolve to the unnamed namespace otherwise, since it has no identifier you can give it to solve the ambiguous resolution problem.

If your code is inside the namespace{} block itself, the local name gets priority over the global one, so a Foo() will call the Foo() within your namespace, and a ::Foo() will call the namespace at global scope.

Kistaro Windrider
A: 

While Georg gives standard-complient, correct, right, and respectable answer, I'd like to offer my hacky one - use another namespace within the anonymous namespace:

#include <iostream>

using namespace std;

namespace
{
namespace inner
{
    int cout = 42;
}
}

int main()
{
    cout << inner::cout << endl;
    return 0;
}
Nikolai N Fetissov
+2  A: 

The only solution I can think of that doesn't modify the existing namespace arrangement is to delegate main to a function in the anonymous namespace. (main itself is required to be a global function (§3.6.1/1), so it cannot be in an anonymous namespace.)

void Foo() // 1
{
}

namespace
{
  void Foo() // 2
  {
  }
}

namespace { // re-open same anonymous namespace

    int do_main()
    {
      Foo(); // Calls local, anonymous namespace (Foo #2).
      ::Foo(); // Calls the Foo in the global namespace (Foo #1).

      return 0; // return not optional
    }

}

int main() {
    return do_main();
}
Potatoswatter