Search code examples
new-operatoreigen3

eigen3: placement new syntax for Map to a const value


Hello all: I'm a bit confused with Eigen3 and new placement syntax because this code work:

double ptr1[9]={1,2,3,4,5,6,7,8,9};
const double ptr2[9]={1,2,3,4,5,6,7,8,9};
Map<Matrix<double,3,3> >    mUseless(ptr1);
new (&mUseless) Map<const Matrix<double,3,3> > (ptr2);
mUseless(1,1)=6.0;

If mUseless now point to a const double, why this code works??


Solution

  • Calling placement new does not change the type of the object, and since mUseless is not a Map<const ...>, it is perfectly normal that you can change the content of the referenced values.

    Actually, your code is similar to a reinterpret_cast from a Map<const Matrix<double,3,3> > to a Map<Matrix<double,3,3> >.