Koenig lookup and template arguments
Consider the following code:
namespace A
{
struct P {};
struct Q {};
template <class T, class U>
U operator&(T, U) { return U(); }
}
namespace B
{
template <class T>
struct X {};
struct Y {};
template <class T, class U>
U operator&(T, U) { return U(); }
}
B::Y y1 = B::X<int>() & B::Y();
B::Y y2 = B::X<A::P>() & B::Y();
The line that declares y1 compiles fine, as expected. But the line
that declares y2 gives an error, saying that operator& is ambiguous,
it could be the one in namespace A or the one in namespace B (with
MSVC 2005, gcc 3.4.2 Cygwin and Comeau online).
I would have thought that a B::X object only lives in the namespace B
since it doesn't derive from a class in another namespace. But
apparently its template argument can make Koenig lookup check the
namespace A. Even the following line gives the same error:
B::Y y3 = B::X<B::X<A::P> >() & B::Y();
This is counterintuitive to me. In my code, I have a similar
situation where the classes in namespace A all have a method
void call(), that is called by operator&. Therefore it is pointless
to use A::operator& with objects from namespace B.
I agreed when Herb Sutter wrote that Koenig lookup doesn't punch
holes in namespaces, until I came across this. What is the rationale
for this behaviour? And what can I do to solve this problem (aside
from using a function or another operator)?
--
Mark dot Van dot Peteghem at q-mentum dot com
http://www.q-mentum.com -- easier and more powerful unit testing
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]