'operator==' 的命名空间限定重载

Namespace qualified overloading of 'operator=='

我很好奇为什么以下代码无法编译:

#include <iostream>
#include <functional>

namespace Bar {
struct Foo {
  int x;
};
}  // Namespace                                                                                                                     

static bool operator==(const Bar::Foo& a, const Bar::Foo& b) {
  return a.x == b.x;
}

int main() {
  Bar::Foo a = { 0 };
  Bar::Foo b = { 1 };

  // The following line is OK
  std::cout << (a == b) << std::endl;

  // The following line is not OK
  std::cout << std::equal_to<Bar::Foo>()(a, b) << std::endl;
}

这种情况下的编译器错误:

[test]$ g++ --std=c++11 -o test test.cc
In file included from /usr/include/c++/4.8/string:48:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from test.cc:1:
/usr/include/c++/4.8/bits/stl_function.h: In instantiation of ‘bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Bar::Foo]’:
test.cc:18:46:   required from here
/usr/include/c++/4.8/bits/stl_function.h:208:20: error: no match for ‘operator==’ (operand types are ‘const Bar::Foo’ and ‘const Bar::Foo’)
       { return __x == __y; }
                    ^
/usr/include/c++/4.8/bits/stl_function.h:208:20: note: candidates are:
...

即使我尝试另一种变体,违规行也无法编译:

namespace Bar {
struct Foo {
  int x;
  bool operator==(const Foo& o) {
    return x == o.x;
  }
};
}  // Namespace

但是,似乎以下确实编译:

namespace Bar {
struct Foo {
  int x;
};

static bool operator==(const Foo& a, const Foo& b) {
  return a.x == b.x;
}
}  // Namespace

这到底是怎么回事?

您的第一个 operator== 是在包含 <functional> 之后声明的,因此在 std::equal_to<Bar::Foo>::operator() 的模板定义上下文中对 operator== 的非限定查找将找不到它。而且它不在 Bar 中,Bar::Foo 的唯一关联命名空间,所以它也没有被 ADL 找到。

第二个,成员,版本barfs,因为equal_tooperator()通过const引用接受两个参数,但成员函数不是const

第三个版本有效,因为 operator==Bar 在同一个命名空间中,所以它可以被 ADL 找到。