C ++ unqualified search

I have the following code:

//mystd plays a role of the std namespace
//which does not allow any new overloads in it
//so we define one in the global namespace
namespace mystd {

template<typename T>
struct A {};

}

//if one uncomment this, everything works as designed
//int f(const mystd::A<int>& v);

//this some namespace which is supposed to be the main one
namespace a {

template<typename T>
int f(const T& v) {
  return 0;
}

template<typename T>
int operator-(const T& v) {
  return 0;
}

}

//this should be different from ::a
//so that `using namespace a;` would import global functions 
//as well as all functions from namespace a, since 
//the global namespace is the nearest common namespace
//see http://www.open-std.org/jtc1/sc22/open/n2356/dcl.html#namespace.udir 
namespace b {

template<typename T>
int Call(const T& v) {
  using namespace ::a;
  return -v + f(v);
}

}

int operator-(const mystd::A<int>&) {
  return 1;
}
int f(const mystd::A<int>&) {
  return 2;
}

int main() {
  //why this returns 1, not 3?
  //what is the difference between operator-() & f()?
  return b::Call(mystd::A<int>());
}

The idea is to mimic the behavior of the gtest <and PrintTo statements so that you can overload the behavior for some std classes, which cannot be done through ADL, because adding anything to the std namespace is not allowed. However, if I try to do this using a regular function f(), then the overload for f(mystd::A())is considered too late (if you comment on the definition of the f()gcc template gives note: β€˜int f(const mystd::A<int>&)’ declared here, later in the translation unit)

The question is why the behavior is different from operator-()and f()?

+4
source share

All Articles