[eigen] writing a function that takes Eigen::MatrixBase or Eigen::Block

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Following the guidance here [0], I have:

template <typename Derived>
void test(const Eigen::MatrixBase<Derived>& a)
{
  // Use case 1:
  Derived aa;
  // do stuff with aa

  // Use case 2:
  Derived bb = a;
  // do stuff with bb
}

If I create an Eigen::Matrix and call test like so, it works fine:

Eigen::Matrix<double, 2, 4> A;
A << 1, 2, 3, 4,
        5, 6, 7, 8;
// Works
test(A);

However, the following doesn't work:
test(A.block<2, 1>(0, 1));

However, the following does work:
test(A.block<2, 1>(0, 1).eval());

Is .eval() the right approach here? Should I being doing something else at the call site, or in my function definition to better handle this? Does the return value of `.block<2, 1>(0, 1)` constitute a "plain matrix or vector (not an _expression_)" [1] or not, so that I can be certain this isn't creating a useless copy? 

Sincerely,
Luke

[0] https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html 
[1] https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#title39


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/