Перегрузка пространства имен, квалифицированная как 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;
}
Компилятор 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:
...
Неправильная строка не компилируется, даже если я попробую другой вариант:
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
Какого черта здесь происходит?
1 ответ
Решение
Ты первый operator==
объявляется после включения <functional>
безоговорочный поиск operator==
в контексте определения шаблона std::equal_to<Bar::Foo>::operator()
не найду его. И это не в Bar
единственное связанное пространство имен Bar::Foo
поэтому он не найден ADL.
Второй, член, версия Barfs, потому что equal_to
"s operator()
принимает оба аргумента по константной ссылке, но функция-член не const
,
Третья версия работает потому что operator==
находится в том же пространстве имен, что и Bar
так он найден ADL.