特征实例包含另一个持有固定大小特征对象的实例 [英] Eigen instance containing another instance holding a fixed-size eigen object
问题描述
我刚刚阅读了具有静态成员的结构本征页面。后者指出以下内容:
如果定义的结构具有固定大小的可矢量化特征类型成员,则必须重载其 operator new ,以便它生成16字节对齐的指针。幸运的是,Eigen为您提供了一个宏EIGEN_MAKE_ALIGNED_OPERATOR_NEW,可以为您完成此操作。
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
宏用于包含其他类实例的类实例,而其他类实例又持有固定大小的容器?
例如,在后续代码段的A类中,是否需要EIGEN_MAKE_ALIGNED_OPERATOR_NEW?
#include< Eigen / Core>
B类
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Eigen :: Vector2d v;
};
类别A
{
public:
B b;
};
int main(int argc,char * argv [])
{
B * b = new B(); //这应该没有对齐问题,因为我们使用EIGEN_MAKE_ALIGNED_OPERATOR_NEW
A * a = new A(); // 这个怎么样?
返回0;
}
解决方案在您的情况下在
A
和B
中。如果A
将从B
继承,则它也将继承new
运算符(因此不必再次编写)。另外,如果B
本身永远不会直接分配,而只是作为A
的一部分,则需要EIGEN_MAKE_ALIGNED_OPERATOR_NEW
仅在A
中使用。
此外,如果编译为在x86_64体系结构中,您的程序也很有可能会工作,因为大多数编译器将始终对
new
的结果进行16字节对齐,而只需添加EIGEN_MAKE_ALIGNED_OPERATOR_NEW
到处都几乎不会影响性能(除非您过多地(取消)分配了对象)。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; }
解决方案In your case it is needed in both
A
andB
. IfA
would inherit fromB
it would also inherit thenew
operator (thus making it not necessary to write again). Also, ifB
itself would never be allocated directly, but just as part ofA
, you would need theEIGEN_MAKE_ALIGNED_OPERATOR_NEW
only inA
.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 addingEIGEN_MAKE_ALIGNED_OPERATOR_NEW
everywhere will barely have a performance impact (unless you excessively (de-)allocate objects).这篇关于特征实例包含另一个持有固定大小特征对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!