Consider this code:
#include <iostream>
namespace A {
struct Mine {};
template <typename T1, typename T2>
void foo(T1, T2)
{
std::cout << "A::foo" << std::endl;
}
}
namespace B {
template <typename T>
void foo(T, T)
{
std::cout << "B::foo" << std::endl;
}
}
using namespace A;
using namespace B;
// or with the same effect:
//using A::foo;
//using B::foo;
int main()
{
A::Mine a;
foo(a, a);
}
The program prints B::foo
instead of A::foo
. Why does it use B::foo
instead of A::foo
?
Imagine the following situation: your library provides a namespace A
and some other library provides namespace B
which includes a function template with the same name foo
as your library. Normally, there is no problem since the correct version can be selected using qualified calls or relying on the argument-dependent lookup. But if the user introduces both A::foo
and B::foo
to the same scope using a using
declaration, the unqualified call is not ambiguous but the wrong function may be selected.
Is there a way to prefer A::foo
over B::foo
, since according to the argument-dependent lookup A::foo
should be selected? What would be your advice in this situation?