命名空间限定'运算符=='的重载

Mok*_*sha 1 c++ argument-dependent-lookup c++11

我很好奇以下为什么不编译:

#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;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下编译器barfs:

[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:
...
Run Code Online (Sandbox Code Playgroud)

即使我尝试其他变体,违规行也无法编译:

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

然而,它似乎像下面这样编译:

namespace Bar {
struct Foo {
  int x;
};

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

到底发生了什么事?

T.C*_*.C. 5

你的第一个operator==是在include之后声明的<functional>,所以operator==在模板定义上下文中 不合格的查找std::equal_to<Bar::Foo>::operator()将找不到它.并且它不是Bar唯一的关联命名空间Bar::Foo,因此ADL也找不到它.

第二,部件,版本barfs因为equal_tooperator()采用由const引用这两个参数,但成员函数不是const.

第三个版本有效,因为它与ADL找到的operator==名称空间相同Bar.