Search code examples
c++eigen

Eigen Vector4d as function argument?


I am attempting to pass an Eigen::Vector4d into an function like this:

  Matrix3d quat2DCM(Vector4d quat)
  {
    quat = quat;
    return Matrix3d::Identity();
    //nevemind the guts of this function, that'l come after this works
  }

the VC++2005 compiler is giving me the following error:

error C2719: 'quat': formal parameter with __declspec(align('16')) won't be aligned

Which does not happen for Eigen::Vector3d objects as arguments. I have noticed that in some online discussion that the Vector4d class is particularly picky about it's alignment, moreso than the other canned typedefs. When using the Vector4d in a class, I found it necessary to use the macro EIGEN_MAKE_ALIGNED_OPERATOR_NEW which overrides the new Is there a similar workaround for passing them s arguments?


Solution

  • According to Eigen's doccumentation, passing fixed sized eigen objects can "be illegal or make your program crash." This is because the alignment modifiers Eigen uses are not respected when the objects are passed by value. You should change your function so that it takes a const reference instead.

    Matrix3d quat2DCM(const Vector4d& quat)
    {
        ...
    }