This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Bug (?) in namespaces and overloading resolution
- To: "Martin v. Loewis" <loewis at informatik dot hu-berlin dot de>, njs3 at doc dot ic dot ac dot uk, egcs-bugs at cygnus dot com
- Subject: Re: Bug (?) in namespaces and overloading resolution
- From: njs3 at doc dot ic dot ac dot uk (Niall Smart)
- Date: Mon, 22 Jun 1998 23:55:59 +0100
On Jun 22, 10:18pm, "Martin v. Loewis" wrote:
} Subject: Re: Bug (?) in namespaces and overloading resolution
> > The following code does not compile on 19980604, I believe it should.
> > It works if the defintion of struct X is moved into namespace B.
>
> Can you be a bit more specific why you think this should work? It
> seems you are aiming at argument-dependent lookup. Given the arguments
> cerr of type std::ostream (in theory) and t of type A::X, the
> associated namespaces of this call are std and A. Neither one defines
> a suitable operator; it doesn't help that one is defined in B.
Hi Martin,
I've appended the code again so that we are definately talking about
the same thing, its a while since I posted this. The template function
C::Y& operator<< <T>(Y&, const T& t) takes an argument of type T, this
is instantiated with T = A::X on line 44 in main(). The body of the
function looks like "cerr << t;". I am expecting it to find B::ostream&
operator<<(ostream&, const A::X&) since I have a "using namespace B;"
declaration, but it does not. However, if I move the definition of
X into namespace B instead of having a "using A::X;" declaration in
namespace B then it works as expected. Surely the behaviour should
be the same in both cases? I don't understand why a function in
C wouldn't find a function in B given that both namespaces are merged
into the global namespace. Are you saying that since the arguments
to C::Y& operator<< <T>(Y&, const T& t) are defined in namespaces
C and A then only namespaces C, A and the global namespace are
used for function lookup despite the presence of a "using namespace B";
declaration?
Niall
#include <iostream.h>
using namespace std;
namespace A {
struct X;
}
namespace B {
using A::X;
ostream& operator<<(ostream&, const X&);
}
namespace A {
struct X { };
using B::operator<<;
}
namespace B {
inline ostream& operator<<(ostream& ostr, const X& x)
{
return ostr;
}
};
using namespace B;
namespace C {
struct Y { };
template<class T>
Y& operator<<(Y& y, const T& t)
{
cerr << t;
return y;
}
};
using namespace C;
int
main()
{
X x;
Y y;
y << x;
}
Niall