help with c++ namespace visibility rules problem

David Mansfield david@cobite.com
Thu Oct 9 18:29:00 GMT 2008


Hi,

I've got the following small test case which fails to compile with g++
(g++ (GCC) 3.4.6 20060404 (Red Hat 3.4.6-9)) - it also fails on other
platforms the same way:

/* -------------------------- t.C ----------------------- */
namespace n1 {
  class C {};
  void f(C& c) {}
}

namespace n2 {
  using n1::C;
  void f(C& c) {}
}

using namespace n2;

void foo(C& c)
{
  f(c);
}
/* ------------------------------------------------------- */

The error is:
t.C: In function `void foo(n1::C&)':
t.C:15: error: call of overloaded `f(n1::C&)' is ambiguous
t.C:8: note: candidates are: void n2::f(n1::C&)
t.C:3: note:                 void n1::f(n1::C&)

Why won't it automatically use n2::f() in this case?  What makes this
ambiguous?  Should the n1 namespace be hidden unless I'm "using" it?

Thanks,
David Mansfield




More information about the Gcc-help mailing list