I am using Eigen library and its block operations, according to https://eigen.tuxfamily.org/dox/group__TutorialBlockOperations.html the blocks are both lvalues and rvalues. But the following program does not compile:
#include "Eigen/Dense"
#include <iostream>
int main() {
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> m(4, 4);
m << 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16;
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> &column = m.col(0);
}
In this program, I want to use column as a reference to the matrix m so I could alter the elements in-place. The error message is:
test_eigen_rvalue.cc: In function ‘int main()’:
test_eigen_rvalue.cc:11:73: error: invalid initialization of non-const reference of type ‘Eigen::Matrix<float, -1, -1>&’ from an rvalue of type ‘Eigen::DenseBase<Eigen::Matrix<float, -1, -1> >::ColXpr {aka Eigen::Block<Eigen::Matrix<float, -1, -1>, -1, 1, true>}’
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> &column = m.col(0);
It seems the issue is that m.col(0) is not an lvalue.
m.col(0)
is an rvalue, not an lvalue. The page is trying to say that you can writem.col(0) = whatever;
to write to the column. – Bernita