I have the following code:
namespace mystd {
template<typename T>
struct A {};
}
namespace a {
template<typename T>
int f(const T& v) {
return 0;
}
template<typename T>
int operator-(const T& v) {
return 0;
}
}
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() {
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()?
source
share