本征实例包含另一个持有固定大小本征对象的实例

Eigen instance containing another instance holding a fixed-size eigen object

我刚刚阅读了 Structures having static members 本征页。后者陈述如下:

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.

但是我不清楚我们是否也应该对 class 实例使用 EIGEN_MAKE_ALIGNED_OPERATOR_NEW 宏,这些实例包含其他 class 实例,而这些实例又包含固定大小的容器?

例如,在以下代码段的 class A 中,是否需要 EIGEN_MAKE_ALIGNED_OPERATOR_NEW?

#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;
}

在您的情况下,AB 都需要它。如果 A 将从 B 继承,它也会继承 new 运算符(因此无需再次编写)。此外,如果 B 本身永远不会被直接分配,而是作为 A 的一部分,您将只需要 A.

中的 EIGEN_MAKE_ALIGNED_OPERATOR_NEW

此外,如果您针对 x86_64 架构进行编译,您的程序也很可能会运行,因为大多数编译器总是 16 字节对齐 new 的结果,另一方面,只需添加EIGEN_MAKE_ALIGNED_OPERATOR_NEW 无处不在几乎不会对性能产生影响(除非您过度(取消)分配对象)。