Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Eigen move arrayXXd to MatrixXd

Tags:

c++

c++11

eigen

I want to move(or swap) an array of type Eigen::ArrayXXd to Eigen::MatrixXd. To achieve this, I tried,

#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main(int , char** ) {
    Eigen::ArrayXXd array(100,100);
    auto mat2 = std::move(mat.matrix());
    cout << array.size() << endl;
    cout << mat.size() << endl;
}

The output shows that the both sizes are 10000, which means array was copied. To avoid copy, I also tried,

    Eigen::MatrixXd mat;
    mat.swap(array.matrix());   (runtime error assert failure)
//  swap(array.matrix(), mat);  (compile error)

The version of Eigen I tested is 3.2.0 beta1 and gcc 4.8.0 was used. From the experiment, it seems that the move semantic for Matrix and Arrays is not implemented yet. Is that right?

Is there a way I can safely move (without copy)?

like image 573
Sungmin Avatar asked May 07 '13 06:05

Sungmin


1 Answers

You cannot force something to be moved. If Eigen does not have a move constructor/assignment operator for the operations you're trying to do (and I see no evidence of Eigen being move-aware in their documentation), then you can't move them.

Move isn't magic; it requires explicit support by the writer of a type.

like image 154
Nicol Bolas Avatar answered Nov 14 '22 03:11

Nicol Bolas