Eigen instance containing another instance holding

2019-08-26 09:08发布

问题:

I 've just read the Structures having static members eigen page. The latter states the following:

If you define a structure having members of fixed-size vectorizable Eigen types, you must overload its "operator new" so that it generates 16-bytes-aligned pointers. Fortunately, Eigen provides you with a macro EIGEN_MAKE_ALIGNED_OPERATOR_NEW that does that for you.

However it's not clear to me whether we should also use the EIGEN_MAKE_ALIGNED_OPERATOR_NEW macro for class instances that hold other class instances which in turn hold the fixed-size containers?

For example, in class A of the followin snippet, is the EIGEN_MAKE_ALIGNED_OPERATOR_NEW needed?

#include <Eigen/Core>

class B
{
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
  Eigen::Vector2d v;
};

class A
{
public:
  B b;
};


int main(int argc, char *argv[])
{
  B* b = new B(); // this should have no alignement problems as we use EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  A* a = new A(); // how about this one?

  return 0;
}

回答1:

In your case it is needed in both A and B. If A would inherit from B it would also inherit the new operator (thus making it not necessary to write again). Also, if B itself would never be allocated directly, but just as part of A, you would need the EIGEN_MAKE_ALIGNED_OPERATOR_NEW only in A.

Also, if you compile for a x86_64 architecture your program will also very likely work, since most compilers will always 16byte-align the result of new, on the other hand, just adding EIGEN_MAKE_ALIGNED_OPERATOR_NEW everywhere will barely have a performance impact (unless you excessively (de-)allocate objects).



标签: c++ eigen eigen3