I'm curious about why the following doesn't compile:
#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;
}
The compiler barfs in this case:
[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:
...
The offending line doesn't compile even if I try another variant:
namespace Bar {
struct Foo {
int x;
bool operator==(const Foo& o) {
return x == o.x;
}
};
} // Namespace
However, it seems like the following does compile:
namespace Bar {
struct Foo {
int x;
};
static bool operator==(const Foo& a, const Foo& b) {
return a.x == b.x;
}
} // Namespace
What the heck is going on here?
Aucun commentaire:
Enregistrer un commentaire