This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Bug (?) in namespaces and overloading resolution


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




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]