4

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 imitate the behavior of gtest operator<< & PrintTo, so that one could overload behaviour for some std classes, which can not be done through ADL since adding anything to std namespace is not allowed. However, if I try to do it with a usual function f(), then the overload for f(mystd::A()) is considered to be defined too late (if one comments the template definition of f() the gcc gives note: ‘int f(const mystd::A<int>&)’ declared here, later in the translation unit)

The question is why the behaviour is different for operator-() and f()?

4

0 回答 0