2

I'm having trouble understanding function resolution across namespaces. In particular, can someone shed some light on some unexpected behavior on the following code?:

namespace Foo {
struct Thing {};
void Bar(int) {}
void Baz(Thing) {}
} // namespace Foo

void Test() {
  Foo::Thing thing;
  Bar(42); // error: 'Bar' was not declared in this scope
  Baz(thing); // Works???
  return 0;
}

In the code above, calling the unqualified Bar does not work, as expected, because it is neither fully qualified (::Foo::Bar) nor implicit (using namespace Foo, or defining Test within the Foo namespace). But for some reason, the unqualified Baz(Foo::Thing) does resolve without error. Can someone explain why the second call is resolved while the first call is not? And what are the general rules for this kind of resolution?

I always thought that if I wasn't within a namespace, or an object of that namespace, then I never had implicit access to that namespace. Obviously I was wrong, at least in one case.

MooseBoys
  • 6,641
  • 1
  • 19
  • 43

0 Answers0