【发布时间】:2018-05-02 00:53:14
【问题描述】:
我终于遇到了这里描述的烦人问题: https://eigen.tuxfamily.org/dox/group__TopicStlContainers.html
我有一个包含几个 Eigen 固定大小矩阵的结构,我想将我的结构的几个实例存储在 std::vector 中。所以很明显,“这些问题只出现在固定大小的可矢量化特征类型和具有这样的特征对象作为成员的结构中。”适用。但是,这里只描述了如何解决向量直接收集特征对象的问题,而不是包含特征实例的结构。
目前我的问题是这样的:
struct MyStruct{
EIGEN_MAKE_ALIGNED_OPERATOR_NEW //Don't know if this applies here
Eigen::Matrix<double, 4, 4> A;
// several more instances and stuff here
MyStruct ( ...) // constructor taking several arguments, but only running an initilization list for some members
// no destructor defined / implemented !
// no methods!
}
//
MyStruct instanceStruct( .. constructing .. );
instanceStruct.A = someAssigment() ;
// this type of 'instancing' before i push it into the vector is necessary ...
std::vector<MyStruct> myVector;
myVector.push_back( std::move( instanceStruct ) ); // gdb tells me that here we run into a SIGSEGV
导致问题的原因是什么?谢谢!
【问题讨论】:
-
你用的是什么编译器? AFAIK,在 c++17 中,一切应该开箱即用,没有 EIGEN_MAKE_ALIGNED_OPERATOR_NEW 之类的......
-
gcc 7.2 (arch linux),带有 -march=native。没有 -march=native,(因此,没有矢量化),一切正常
-
好的,但是你试过“-std=c++17”吗?
-
行得通,谢谢!但是,我能为 C++11 标准做什么?
-
在 c++11 中,您需要使用特征提供的分配器及其特征提供的向量特化。这很丑,我知道。要么,要么禁用矢量化,要么将 MyStruct 作为 unique_ptr 存储在另一个结构中,然后您可以将其传递给矢量...即便如此,您仍然需要特别注意在复制特征对象时避免 UB ...我会稍后写一个更详细的答案...