次のコードがあります。
//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>());
}
gtest operator<< & PrintTo の動作を模倣して、std 名前空間に何かを追加することは許可されていないため、ADL では実行できない一部の std クラスの動作をオーバーロードできます。ただし、通常の関数でそれを実行しようとするとf()
、のオーバーロードのf(mystd::A())
定義が遅すぎると見なされます ( f()
gccのテンプレート定義にコメントがある場合note: ‘int f(const mystd::A<int>&)’ declared here, later in the translation unit
)
問題は、なぜ と の動作が異なるのかということoperator-()
ですf()
。