我刚刚阅读了 Structures having static members Eigen 页。后者陈述如下:
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.
但是我不清楚我们是否还应该对包含其他类实例的类实例使用 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
宏,而这些实例又包含固定大小的容器?
例如,在以下代码段的 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;
}
最佳答案
在您的情况下,A
和 B
都需要它。如果 A
继承自 B
,它也会继承 new
运算符(因此无需再次编写)。此外,如果 B
本身永远不会被直接分配,而只是作为 A
的一部分,则您只需要在 A 中使用
。EIGEN_MAKE_ALIGNED_OPERATOR_NEW
此外,如果您为 x86_64 架构编译,您的程序也很可能会工作,因为大多数编译器总是 16 字节对齐 new
的结果,另一方面,只需添加 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
几乎不会对性能产生任何影响(除非您过度(取消)分配对象)。
关于c++ - Eigen 实例包含另一个持有固定大小 Eigen 对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49075375/