I'm trying to compile the following code:
#include <boost/geometry/geometries/point_xy.hpp>
#include <iostream>
#include <utility>
typedef boost::geometry::model::d2::point_xy<long> Point;
typedef std::pair<Point, Point> Vector;
bool operator==(const Point& p1, const Point& p2) {
return p1.x() == p2.x() && p1.y() == p2.y();
}
int main() {
Vector vec1(Point(0,0), Point(1,1));
Vector vec2(Point(0,0), Point(1,2));
std::cout << ((vec1 == vec2) == false) << std::endl;
std::cout << ((vec1 == vec1) == true) << std::endl;
}
VS2012 C++ compiler returns the following compilation error:
...VC\include\utility(219): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const Point' (or there is no acceptable conversion)
GCC C++ compiler returns the following compilation error:
/usr/include/c++/4.8/bits/stl_pair.h:
In instantiation of ‘bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = boost::geometry::model::d2::point_xy; _T2 = boost::geometry::model::d2::point_xy]’:
test.cpp:22:28: required from here /usr/include/c++/4.8/bits/stl_pair.h:215:51: error:
no match for ‘operator==’ (operand types are ‘const boost::geometry::model::d2::point_xy’ and ‘const boost::geometry::model::d2::point_xy’) { return __x.first == __y.first && __x.second == __y.second; }
Error disappears if I overload == operator for Vector:
bool operator==(const Vector& v1, const Vector& v2) {
return v1.first == v2.first && v1.second == v2.second;
}
The reason why this fails is that the operator ==
for std::pair
uses ==
to compare the pairs' members, which in turn uses argument-dependent lookup (ADL) to find the proper operator ==
for them. But you've provided the overload in the wrong namespace, since Point
is actually a typedef for something in ::boost::geometry::model::d2
, and not in ::
.
If you move the operator into the correct namespace (which is a good idea anyway), it works:
#include <boost/geometry/geometries/point_xy.hpp>
#include <iostream>
#include <utility>
typedef boost::geometry::model::d2::point_xy<long> Point;
typedef std::pair<Point, Point> Vector;
namespace boost { namespace geometry { namespace model { namespace d2 {
bool operator==(const Point& p1, const Point& p2) {
return p1.x() == p2.x() && p1.y() == p2.y();
}
} } } }
int main() {
Vector vec1(Point(0,0), Point(1,1));
Vector vec2(Point(0,0), Point(1,2));
std::cout << ((vec1 == vec2) == false) << std::endl;
std::cout << ((vec1 == vec1) == true) << std::endl;
}