Re: [eigen] Block sparse matrices and std vector compilation error

[ Thread Index | Date Index | More Archives ]

On 26.11.2014 13:04, Hilario Tomé wrote:
I am interested in storing block sparse matrices in std vectors but get a
compiling error, the error does not happen for dense matrices. I am using
Eigen 3.2.2 with gcc version 4.6.3

Block<> matrices do not have a copy-assignment operator, which is required for std::vector elements in C++03 (not in C++11 anymore), so even for dense matrices you could get undefined behavior (I just checked with g++-4.7 and it appears to work fine).

Block<Matrix> does have an operator= defined, which however, does not copy the Block object itself, but overwrites the values in the referenced matrix. But even though g++ checks for its existence it does not appear to use this anyways. So your case with MatrixXd instead of SparseMatrix<double> does work, but at least for C++03 I would not rely on it to work correctly everywhere.

If you use Block<const MatrixXd> everywhere instead of Block<MatrixXd> then the assignment:
  MatrixXd A;
  Block<const MatrixXd> block = A.block(0,0,5,5);       // (*)
does not work, however this works:
  const MatrixXd A;
  Block<const MatrixXd> block = A.block(0,0,5,5);
Then (for g++) the rest of your program works in C++11 mode, but not in C++03 mode (because Block<const MatrixXd> does not have an assignment operator).

Overall, you might want to reconsider your design. Block objects are somewhat of an internal thing, and the type names might probably change in the future (e.g., SparseMatrix::middleCols(...) could theoretically return a MappedSparseMatrix).


We should probably add a constructor of Block<const X> from Block<X> to make (*) work. OTOH, explicitly storing Block objects is not good style anyways, IMO.

For Block<SparseMatrix> we should probably make it more clear that it does not have an assignment operator at all (implement an operator= which raises a static assertion). And for Block<*> it would be nice if we could tell the compiler that it is not actually copy-assignable (I'm afraid, there is no easy way to tell this without having C++-concepts (which did not even make it into C++11)).



Dipl.-Inf., Dipl.-Math. Christoph Hertzberg
Cartesium 0.049
Universität Bremen
Enrique-Schmidt-Straße 5
28359 Bremen

Tel: +49 (421) 218-64252

Mail converted by MHonArc 2.6.19+