I want to use an auto reference to a block of an eigen matrix:
#include <Eigen/Dense>
using namespace Eigen;
void foo(MatrixXf& a)
{
auto& a_block = a.block(2, 3, 4, 5);
a_block = MatrixXf::Random(4,5);
}
This does not compile with GCC, since a.block(2, 3, 4, 5)
is evaluated into a temporary, while a.block(2, 3, 4, 5) = MatrixXf::Random(4,5);
works perfectly.
From my point of view this is not expected behaviour. Is there an elegant fix to this problem? Should this be considered a bug / feature request to Eigen?
EDIT:
using auto
instead of auto&
solves the problem!
The question has been marked as a duplicate of Reference a temporary in msvc, but it has nothing to do with MSVC. I also made clear that it's obvious that a.block(2, 3, 4, 5)
is evaluated into a temporary. The question was about whether this is correct behaviour of Eigen.