CGAL gotcha when trying to modify coordinates of a point etc.

Alec Jacobson

December 12, 2015

weblog/

Today I ran into a CGAL gotcha. If you try to directly modify, say, the x-coordinate of a point like this:

#include <CGAL/Point_2.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <iostream>
int main(int argc, char * argv[])
{
  using namespace std;
  using namespace CGAL;
  Point_2<CGAL::Epeck> A(0,0);
  Point_2<CGAL::Epeck> B(1,2);
  A.x() = B.x();
  cout<<"A: "<<A.x()<<","<<A.y()<<endl;
}

The program will silently compile without warning or error, but the run time behavior is not what you'd expect. The program prints:

A: 0,0

That is, A was unchanged by the line A.x() = B.x();

The CGAL doc suggests instead always creating a new point: A = Point_2(B.x(),A.y()); Too bad.