0

I want to use an auto reference to a block of an eigen matrix:

MatrixXf a = MatrixXf::Random(20,20);
auto& a_block = a.block(2, 3, 4, 5);

MSVC compiles and works well, while GCC does not compile, if the reference is not constant. See the example here

The error from GCC is

[x86-64 gcc 8.3 #2] error: cannot bind non-const lvalue reference of type 'Eigen::Block<Eigen::Matrix<float, -1, -1>, -1, -1, false>&' to an rvalue of type 'Eigen::DenseBase<Eigen::Matrix<float, -1, -1> >::FixedBlockXpr<-1, -1>::Type' {aka 'Eigen::Block<Eigen::Matrix<float, -1, -1>, -1, -1, false>'}

Is this a bug or should it be expected behaviour? Is there an elegant workaround for GCC?

Here's the linked code of the reproducible example (for archieve):

#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);
}    
yar
  • 1,855
  • 13
  • 26
  • This is also a typical question that requires a [mcve]. – Ulrich Eckhardt Mar 17 '19 at 11:46
  • @UlrichEckhardt it's there! Just click on "here" – yar Mar 17 '19 at 11:47
  • 2
    "Make sure all information necessary to reproduce the problem is included in the question itself" -- don't link code in external resources that may or may not change or be deleted. BTW: Extracting the necessary parts from Eigen is IMHO also good style to ensure whether Eigen is relevant or not. – Ulrich Eckhardt Mar 17 '19 at 11:50

0 Answers0