Search code examples
c++arraysstdeigen

Array or vector declaration for aligned datatype Eigen::Matrix


I have an class ClusterNode which contains an Eigen::Matrix4d as a class variable. I also have a function numNodes() which tells me the total number of ClusterNodes, so that I can collect them in some sort of array, list, or vector.

However, Eigen::Matrix4d is aligned which means I cannot store objects of this type in a std::vector<ClusterNode> as per the answer to this question error C2719: '_Val': formal parameter with __declspec(align('16')) won't be aligned?

As an alternative, I have tried using an array.

However, I cannot do

const int n = numNodes();
ClusterNode array [n];

Because the return value of a function is not considered a constant.

What other options do I have?


Solution

  • You can use Eigen's aligned_allocator. See this page for the details, but basically you can do:

    std::vector<ClusterNode, Eigen::aligned_allocator<ClusterNode> >
    

    Also don't forget to overload operator new for your class. Again, all the details are in the Eigen's documentation.